Some problem with using post in jquery

Some problem with using post in jquery

Hi,
 I am using 1.10.4

I am trying to login to my mediawiki api, through a jquery script. This requires me to send 2 Post requests one with the username and password and second with the token received with the first request.

When I do these 2 post requests, using the Chrome REST-client extension, I receive a success result in the 2nd post response.

But using the jquery script, I am unable to do this. I mean instead of a success, i receive a NeedToken on my second response, which should actually give me success.

This is what my first response returns in both the jquery script and the Chrome REST Client

{"login":{"result":"NeedToken","token":"37b4b5893b358884406c3b5125c49071","cookieprefix":"vanisource_vanisource_","sessionid":"cbbe4723035434728ee15fcbe8402921"}}


and second response should be something like this( from Rest client)
{"login":{"result":"Success","lguserid":10,"lgusername":"Test","lgtoken":"8cbe78566c9ac67b41ea82bf54fc169e","cookieprefix":"vanisource_vanisource_","sessionid":"dbc0d6ca3c7882f7058784cbf9c29b7d"}}

but i receive this from the script
  1. login{result:NeedToken, token:37b4b5893b358884406c3b5125c49071, cookieprefix:vanisource_vanisource_,…}
    1. cookieprefix"vanisource_vanisource_"
    2. result"NeedToken"
    3. sessionid"cbbe4723035434728ee15fcbe8402921"
    4. token"7898b5893b358884406c3b5125c492547"
  1. Here is my script
  2. <!DOCTYPE html>
    <html lang="en">
    <head>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

    <script>
    function wiki_auth(login, pass, ref){
        $.post('http://vanisource.org/w/api.php?action=login&format=json',
                {
                    lgname: login,
                    lgpassword: pass,
                    lgdomain:'vanisource.org'

                },
                function(data) {
                console.log(data.login.sessionid);
                var myresult = data.login.result;
                console.log(myresult);
                if(data.login.result == 'NeedToken') {
                $.post('http://vanisource.org/w/api.php?action=login&format=json',
                {
                    lgpassword: pass,
                    lgname: login,
                    lgtoken: data.login.token, 
                    lgdomain:'vanisource.org'
                },

                function(data1) {
               
               console.log(data1.login.result);
               console.log(data1.login.sessionid);
                }
                );

    }
    }
    )};
    </script>

    <script>

    $(document).ready(function(){
       wiki_auth('test', 'wrongpassword', '/w/');
    });
    </script>

    </head>
    <body>
    </body>
    </html>


The MW documentation also says something which i did not understand

Handle cookies[edit | edit source]

A successful action=login request will set cookies needed to be considered logged in. Many frameworks will handle these cookies automatically (such as the cookiejar in cURL). If so, by all means take advantage of this. If not, the most reliable method is to parse them from the HTTP response's Set-Cookie headers.


Does jquery have a way of handling cookies? What does the above mean in regards to jquery? I am not sure if this has something to do with my problem, but i am not sure, what it means?


Thanks in advance

Arnab