Good day!

I make such a simple query (using jQuery):

$.ajax({ type: 'GET', url: adrServ.value + "/auth/login?username=" + login.value + "&password=" + password.value, dataType: "jsonp", crossDomain: true, contentType:"application/json", //cache:false, success: function(data){var dat = data.AuthId; localStorage["uid"] = dat;}, error:function(){$.unblockUI();} }); alert(localStorage["uid"]); 

Actually the last line displays null. If after this line once again register output data storage, then everything is OK. I can not understand what kind of nonsense. Who can prompts, the error in the code can? Work with localStorage is supported (in the browsers tested, all the rules.)

    1 answer 1

    I had this when ajax success had not yet managed to write something somewhere, but I already did an alert. You, hike, also ...

    Play with the async option. I do not remember exactly if it will help ... And you can set an alert delay, for example, at 300 ms. But it's not right.

    I would make a function, for example:

     GoodRequest() { alert(localStorage["uid"]); } 

    and called her like this:

     $.ajax({ type: 'GET', url: adrServ.value + "/auth/login?username=" + login.value + "&password=" + password.value, dataType: "jsonp", crossDomain: true, contentType:"application/json", //cache:false, success: function(data){var dat = data.AuthId; localStorage["uid"] = dat; GoodRequest();}, error:function(){$.unblockUI();} }); 
    • If you just need to see what comes in, you can add an alert and add it to success, and then for sure it will not work before. - MasterAlex
    • Also inclined to this version. Added async: false and timeout: 30000 to the query. It seems that the flight is normal. Thank! - ghost rider
    • @MasterAlex, I wrote if you need to separate one logic from another, but you can write all the code in success, and there should be good) - Mike
    • Better to use promises api.jquery.com/promise/#promise1 - Pavel S. Zaitsau
    • OK. Thanks, you will have to try. - ghost rider