Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery.getJSON not alerting json

I currently running in localhost:

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

http://localhost/call returns {something:1}, yet nothing is alerted.

like image 898
Babiker Avatar asked Feb 24 '26 17:02

Babiker


1 Answers

{something:1}

Isnt a valid JSON string, however

{"something":1}

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 should end up in the error callback.

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);
like image 125
Johan Avatar answered Feb 26 '26 08:02

Johan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!