1

I have a Javascript like below..

    <!DOCTYPE html>
    <html>
    <head>
    <script src="C:\User\Sample\src\jquery-1.8.2.min.js" type="text/javascript"></script>

    <script>
    $(document).ready(function(){
    var sessiontoken;
    var supportCors = $.support.cors;
    var sessiontoken ;
    $.support.cors = true;

    $.ajax({
               type: 'POST',
               url: "https://sessionmanagerg.abc.com/xyz/obj/Session",
               dataType: "json",
               data: {
                   UserId: "TestUser" + "101818",
                   CSK1: "csk1",
                   CustId: "custid1"
               },
        success: function (data) {
                   $.support.cors = supportCors;
                 sessiontoken=data.Token;
        alert(sessiontoken);
               },
               error: function (xhr, textStatus, error) {
                   $.support.cors = supportCors;
                   alert("responseText: " + xhr.responseText);
                   alert("XHR statusText: " + xhr.statusText);
                   alert("textStatus: " + textStatus);
                   alert("error: " + error.message);
               }


    });
    });


    </script>
    </head>
    <body>

    </body>
    </html>

It generates session token after running in IE,chrome(but somehow not in mozilla). I would like to run this script by ScriptEngine of Rhino or Javax.script (jdk 1.6) Api's. I tried above method using Javax.script but i'm getting an error like "$" symbol is not recognized even though i have loaded jquery-1.8.2.min.js file using engine.eval("..");

or

It would be great if i can generate entirely a new java code which does the same functionality of above javascript.Can anyone provide some pointers on this?

Apurv
  • 3,615
  • 3
  • 28
  • 49
User 4.5.5
  • 1,281
  • 3
  • 12
  • 20

1 Answers1

0

One of the challenges in implementing scripts like this is that a javax.script or Java/Rhino runtime does not contain the browser's DOM structure (including the ajax XMLHttpRequest construct). However, it is possible to recreate this using server-side scripting environments. One of these is called env.js which was introduced here and has been maintained here. I have used this package for implementing js tests that execute in a browser-like analogue so I can attest to its effectiveness for scripts like yours.

I have also heard that, though have not personally used, phantomjs works well for the same thing, so you might want to look at that as well.

Nicholas
  • 15,306
  • 4
  • 35
  • 62