简体   繁体   English

JSON输出中的未知错误格式

[英]Unknow error format in JSON output

I have a problem with JSON output. 我有JSON输出的问题。 I created an ajax post like this one: 我创建了一个像这样的ajax帖子:

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). 问题可能与魔术引号有关,或者您正在使用模拟此效果的系统(例如WordPress)。

stripslashes will remove the slashes from the input. stripslashes将从输入中删除斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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