1
function foobar(){
    $.ajaxSetup ({cache: false});
    var response = $.get("http://mysite.com/return.php")
    document.write(response);
}

I'm trying to save the response from this as a variable. How can I do that?

4
  • 3
    Welcome to the world of async! You can't do that.
    – SLaks
    Oct 11, 2011 at 13:47
  • Sorry if it wasn't explained very well, but using the async: false, it works.
    – joedborg
    Oct 11, 2011 at 14:58
  • Do not use async: false! It will freeze the browser.
    – SLaks
    Oct 11, 2011 at 15:44
  • It doesn't. Tried it on all.
    – joedborg
    Oct 11, 2011 at 15:49

4 Answers 4

3

$.get is an asynchronous AJAX call - your document.write line is going to execute long before the server even sends you a response.

If you want to access the return value, you'll need to do it in a callback, like so:

$.get('/return.php', function(response) {
  document.write(response);
});
7
  • so everything must be in there?
    – joedborg
    Oct 11, 2011 at 13:49
  • @jdborg Yes, that's how asynchronous programming works. The get function returns before the response comes back.
    – Skilldrick
    Oct 11, 2011 at 13:50
  • 3
    i'm giving you a -1 as a flag because you didn't notice that document.write is being called asynchronously, which will mean that the entire page will be overwritten.
    – zzzzBov
    Oct 11, 2011 at 13:52
  • 3
    Such a shame that many people seem to be using AJAX without the faintest clue about what it is. Oct 11, 2011 at 13:52
  • Tomalak we all have to start somewhere. Oct 11, 2011 at 13:58
1

You wont be able to do that unless you use $.ajax with the async option set to false. Otherwise, the callback used for AJAX will execute after the document is closed for writing, and you'll overwrite your entire page.

Instead, use load to dump obligatory HTML into the page asynchronously.

6
  • I don't want to load a page using it, I want to use it to return a variable (mysql / glob that kind of thing).
    – joedborg
    Oct 11, 2011 at 13:53
  • @jdborg, load isn't loading a page. It's adding HTML content to the page. document.write is adding HTML content to the page as well; if you're simply trying to get a variable from the server, you're doing it very wrong.
    – zzzzBov
    Oct 11, 2011 at 13:54
  • then please explain a better way.
    – joedborg
    Oct 11, 2011 at 13:57
  • @jdborg, return a JSON object from the server, and the first parameter of the success callback is the variable. If you need to store it globally, you could use function(data){window.globalpollution=data;} as a success callback.
    – zzzzBov
    Oct 11, 2011 at 13:59
  • I'm trying to run a glob using php. So all the return.php does is give a list of file names. I can't see how I can do that using this.
    – joedborg
    Oct 11, 2011 at 14:01
0

$.get provides a success() function for handling the response...

function foobar(){
    var response = null;
    $.ajaxSetup ({cache: false});
    $.get("http://mysite.com/return.php").success(function(respText) { 
            response = respText; 
            document.write(response);
    });
}
0

You can use the $.load() function for this as this is it's intended purpose

See http://api.jquery.com/load/

From the manual:

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data. This means that most uses of the method can be quite simple:

$('#result').load('ajax/test.html');
3
  • I don't want to load a page using it, I want to use it to return a variable (mysql / glob that kind of thing).
    – joedborg
    Oct 11, 2011 at 13:53
  • This will complete the equivalent of your original post including the document.write bit. However, it wont store it in a variable. If you're not intending to write the response to the page then load() is indeed unsuitable, but then equally your OP is misleading. Oct 11, 2011 at 13:56
  • No, I said I wanted to save it as a variable.
    – joedborg
    Oct 11, 2011 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.