C. Hediger C. Hediger - 17 days ago 5
Javascript Question

jquery get() does not return any response

I actually try to load a php script and get some return values.

I tried the following code:
http://www.w3schools.com/code/tryit.asp?filename=FA39VK30JU3U

It does the GET request correctly as i can see with firebug.
But it never shows me any response with the alert box.

I have also tried the following code:

$.ajax({
type: "GET",
url: "test.php",
dataType: "html"
}).done(function (res) {
// Your `success` code
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("AJAX call failed: " + textStatus + ", " + errorThrown + " - " +jqXHR.status);
});


This returns ERROR undefined code 0

I hope someone see the mistake.
Thanks!

Update:

This is my original code:

$.get( "test.php", function( data ) {
alert( "Data Loaded: " + data );
});


This also does not work!

This is my test.php:

<?php

echo "Hellllooouuu";

?>

Answer

Try to use done callback, see this:

http://www.w3schools.com/code/tryit.asp?filename=FA3BI1524HTC

About jqXHR Object:

"As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," returned by $.get() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see Deferred object for more information). The jqXHR.done() (for success), jqXHR.fail() (for error), and jqXHR.always() (for completion, whether success or error; added in jQuery 1.6) methods take a function argument that is called when the request terminates.

The Promise interface also allows jQuery's Ajax methods, including $.get(), to chain multiple .done(), .fail(), and .always() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately."

https://api.jquery.com/jquery.get/