return responseText from jQuery.get() return responseText from jQuery.get() ajax ajax

return responseText from jQuery.get()


After some testing, I ended up finding a solution.

I need the call to be synchronous, $.get shorthand function is always asynchonous, so I will need to use $.ajax, like this:

var msg = $.ajax({type: "GET", url: "my_script.php", async: false}).responseText;

I don't think there is a better way to do this, thanks for your answers.


You can always use:

var msg;$.get("my_script.php", function(text) {  msg = text;});

If for some reason the response is text, the remote script might be changing the content-type to something like JSON, and thus jQuery tries to parse the string before outputting to you.


The return value is simply the jqXHR object used for the ajax request. To get the response data you need to register a callback.

$.get("my_script.php", function(data) {  var msg = data;  alert(msg);});