I found myself needing a really quick way to do an ajax call and get the value returned to a single var. I did a lot of searching and found a lot of convoluted responses that involved callbacks. Using callbacks would be overkill for what I was doing. After much searching I finally found this jQuery API comment. The comment explains how to just get a single value returned from an ajax call. Here is the key points:
- Make sure the call is synchronized. (async: false) This will allow you to wait for the response and not assign a blank value.
- Understand that when you make the $.ajax() call this the data that is normally passed to the callbacks is stored in $.ajax().responseText.
var response = $.ajax({ type: "GET", url: "check.php", async: false }).responseText;
Leave a Reply