Skip to content
Advertisement

jQuery AJAX – Not receiving JSON data when on localhost using XAMPP

I’m using this code:

$.ajax({
    type: 'post',
    url: "http://www.localhost/do_getmemes.php",
    dataType: 'json',
    data: {userid: userid, lastid: lastID},
    success: function(data) {
        console.log('bla');
        console.log(data);
    }
});

inside do_getmemes.php the post parameters are received successfully and the json is getting generated but I don’t get it on success?? Console isn’t showing anything. It works fine on the website but not when on localhost using XAMPP

It all works inside the php file, this is at the end:

file_put_contents('test.json', json_encode($array)); // file generated and not empty
echo json_encode($array);

What’s the problem here?

EDIT:

AJAX usually works, I tested by getting simple string:

$.ajax({
    url: "http://www.localhost/contact/text.php",
    success: function(data) {
        console.log(data) // got it
    }
});

Advertisement

Answer

The problem were irrelevant warnings which were also sent through the API back and causing parsererror SyntaxError: Unexpected token < in JSON at position 0 error.

Besides fixing them this is the way to ensure the APIs wills till work:

Disable the warnings inside the PHP file:

error_reporting(0); 
ini_set('display_errors', 0);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement