0
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?

1

3 Answers 3

1

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)
});
Sign up to request clarification or add additional context in comments.

1 Comment

If the first test didn't worked that means you have a problem with your script php. The request you make is on the same domain ?
0

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

6 Comments

it has header('Content-Type: application/json; charset=utf-8');
@Happy, well then everything seems to be fine. That's very weird. Does FireBug show a JSON tab in the response?
tryed to remove, nothing changes
@Darin Dimitrov no, there are 3 tabs under the request value site.com/json.php, headers are sent, but the answer tab is empty
@Happy, there seems to be some problem with the server. There must be a Response tab and a JSON tab.
|
0

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.

4 Comments

I've noticed firefox response, json code seems to be valid, checked on the jsonlint.com
So you can see the proper JSON being returned in the Response tab?
there is no json tab and no answer from the file request.
I would try replacing the absolute path, with a relative one. So instead of "/json.php" you could do just "json.php" if the php file is in the same directory as your calling page.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.