jQuery: How can I store the JSON response values in variable?
-
My script as follows: var initilization; function onshow(fileid) { $.ajax({ "url": 'GetTags.ashx?fileid=' + fileid, "dataType": "text", "success": function(result) { initilization = result; alert('success result=' + initilization); } }); alert('initilization=' + initilization); } But the execution results are: success result=Cloud Computing initilization=undefined It looks that the response values can't be stored in variables. How can I store the JSON response values in variable via JS? Thank you.
-
Answer:
It is stored, but the alert statement following he $.ajax call is executed before the assignment takes place. It happens because the success callback is called when a result comes from an AJAX call (i.e. asynchronously). I.e. a result of AJAX request should be processed inside a calback. However, you can store them in a global variable and access data later (but make sure, it happens after an assignment had been performed).
Eugene Retunsky at Quora Visit the source
Related Q & A:
- How can I retrieve the object properties values from the database?Best solution by Stack Overflow
- How can I implement an atomic incr and decr on top of an eventually consistent key-value store?Best solution by Stack Overflow
- How can I deserialize JSON {name, value} in C#?Best solution by Stack Overflow
- How can I promote my new online store?Best solution by Yahoo! Answers
- How can I pay on iTunes store with Paypal?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.