这里我使用简单的html+jquery+ajax文件
ajax代码在这里
var email = "[email protected] /cdn-cgi/l/email-protection";
var username = "ankur_07";
var password = "pass07";
var phone_no = "7676715797";
var datastring = {
"email": email,
"username": username,
"password": password,
"phone_no": phone_no
};
$.ajax({
type: "POST",
url: "../test/testreg.php",
data: {datastring : JSON.stringify(datastring)}, //with the page number as a parameter
dataType: 'html', //expect html to be returned
async: false,
success: function (data) {
alert(data);
/*if(data=="hello"){
message = "Mail Sent Successfully";
} else {
message = "Oops, mail doesn't send.!!!";
}
alert(message);*/
}
});
return false;
我从这个文件发送 json 并 ant 进入另一个文件testreg.php
文件
但它没有解码 json,它显示如下行,使用 inecho
{\"email\":\"[email protected] /cdn-cgi/l/email-protection\",\"username\":\"ankur_07\",\"password\":\"pass07\",\"phone_no\":\"7676715797\"}
if(isset($_POST['datastring']))
{
$data = $_POST['datastring'];
$data = json_decode($data);
print_r($data);
}
请帮助我摆脱这个困境..
Replace
data: {datastring : JSON.stringify(datastring)},
by
data: {datastring : datastring},
解释:
您没有发送 JSON。您发送转义字符串(删除 JSON 格式),因为您使用JSON.stringify
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)