简体   繁体   中英

Unknow error format in JSON output

I have a problem with JSON output. I created an ajax post like this one:

function sign_up(form_data)
{
        $.ajax({
        url: "signup_process.php", //target
        data: {userdata:JSON.stringify(form_data)}, 
        type: "POST", //metode pengiriman
        dataType: "json", //return data type

    }).done(function(data) {
        console.log(data);
    }).fail(function(data, errorThrown, textStatus, jqXHR){
        console.log(textStatus);

    });
}

then process it in server side :

$data = json_decode($_POST['userdata'], true);
echo json_encode($_POST['userdata']);die();

In my local server , it run smoothly and display desired output like this one:

{"email":"myemail@gmail.com","browser_agent":"chrome","browser_version":"30.0.1599.69","os":"win","device":"PC/Laptop/non-mobile-device","latitude":-6.211544,"longitude":106.84517199999999,"location":"Padang, Setiabudi, South Jakarta City, Jakarta 12850, Indonesia","ip":"139.xxx.xxx.xxx"} 

But, when i upload it on live web server, the result became like this (so many backslash). and i found it broke my entire code :

{\"email\":\"myemail@gmail.com\",\"browser_agent\":\"chrome\",\"browser_version\":\"30.0.1599.69\",\"os\":\"win\",\"device\":\"PC/Laptop/non-mobile-device\",\"latitude\":-6.211544,\"longitude\":106.84517199999999,\"location\":\"Padang, Setiabudi, South Jakarta City, Jakarta 12850, Indonesia\",\"ip\":\"139.xxx.xxx.xxx\"}

I really have no idea what is really goin on because the code in my local is 100% same with the code on host server. Any solution will be very appreciated. Thanks

Try this:

$data = json_decode(stripslashes($_POST['userdata']), true);
echo json_encode($_POST['userdata']);die();

The issue is probably related to magic quotes or you're using a system that simulates this effect (eg WordPress).

stripslashes will remove the slashes from the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM