0

I think I found bug In latest jQuery 3.3.1. My problem looks like this - when I try to send index as number 0 or string 0 using getJSON function I doesn't get any response. Trying other numbers or string and works perfectly.

My js:

var index = 0;
$.getJSON('/ajax.php' , { index: index }, function(r) {
   console.log( r );
}, 'jsonp');

My example php:

header('Content-Type: application/json');
$index = intval( $_GET['index'] );
file_put_contents('log.txt' , $index);
die(json_encode([
    'received' => $index
]);

In log.txt i found 0 and any other numbers that I send to, but if 0 is as index I didn't see in my console.log json response {received: 0}

  • 1
    FYI `$.getJSON` does not accept a fourth "type" parameter where you are using `'jsonp'`. Try adding a `.fail` handler ~ `.fail(function(jqXhr, status, error) { console.error(status, error) })` – Phil Jan 15 '19 at 23:52
  • Also, check your _Network_ console for the AJAX request. Does the URL have `?index=0`? – Phil Jan 15 '19 at 23:53
  • Whatever the problem is, it's probably in your PHP code. [Cannot reproduce this at all](https://jsfiddle.net/philbrown/f0seqta5/3/) – Phil Jan 16 '19 at 00:03
  • Using browser request ?index=0 working very good. Fail is strange for me: [Error] ERROR: parsererror SyntaxError: JSON Parse error: Unrecognized token '<' When I before die wrote die($index); I see 0 and no error at all. It is worth to say, that I try to remove 'jsonp' and result is same. – Grzegorz Miśkiewicz Jan 16 '19 at 00:05
  • 1
    Look at the actual response in your _Network_ console. Looks like your PHP script is responding with HTML – Phil Jan 16 '19 at 00:06
  • Also, you have a typo; you're missing an extra `)` at the end of your PHP script though this does not explain why sending a non-zero `index` parameter _works_. With that typo in place, nothing should work – Phil Jan 16 '19 at 00:09
  • 1
    Thank You Phill ... I've looking for this a long time - I found solution :-) I save php file as UTF-8 without BOM and now working ! Thank You a lot !! – Grzegorz Miśkiewicz Jan 16 '19 at 00:12

0 Answers0