JQuerygetJSON does not alert json

I am currently working at localhost:

$.getJSON("http://localhost/call", function(json) {
  alert(json.something);
});

      

http://localhost/call

returns {something:1}

, but nothing is warned.

+3


source to share


1 answer


{something:1}

      

Not valid JSON , however

{"something":1}

      

there is.

If you replace your call with



$.ajax({
    url: 'http://localhost/call',
    dataType: 'json',
    success: function(){},
    error: function(xhr, textStatus, errorThrown){
         //you should get a parse error and end up here
    }
});

      

you have to enter the callback error

.

In your php file:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$arr = array('something' => 1, 'somethingelse' => 2);

echo json_encode($arr);

      

+8


source







All Articles