Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
    }
});
question from:https://stackoverflow.com/questions/65946433/jquery-ajax-not-receiving-json-data-when-on-localhost-using-xampp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
102 views
Welcome To Ask or Share your Answers For Others

1 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...