James
James

Reputation: 43647

Ajax "Failed to load resource"

jQuery('input').live('click',function(e){
    $.getJSON(
        "/json.php",
        function(data){
            the_name = data.name;
        }
    );

});

When we press , it should make a json query.

Bit it gives errors.

In Google Chrome console:

In Firefox console:

The strange is when I open http://site.com/json.php, browser gives me a normal json code like: {"name":"Mary"}. It is encoded with php json_encode();

What is the problem?

Upvotes: 0

Views: 7641

Answers (3)

Erikk Ross
Erikk Ross

Reputation: 2183

Load up Firebug and check the request and response using the Console. Make sure the request is getting sent properly and that the response from the server is properly formatted JSON.

Upvotes: 0

alexl
alexl

Reputation: 6851

maybe your json string is not correct:

try

$.get("/json.php", function(data) {alert(data)});

if you see you data in the alert box try:

$.get("/json.php", function(data) {
  var obj = $.parseJSON(data);
  alert(obj.name)
});

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

Your json.php script didn't set the Content-Type: application/json HTTP header?

Upvotes: 0

Related Questions