JSON:位置0的JSON中的意外令牌o
问题描述:
我收到此错误来自昨天的位置0的JSON中的意外令牌o尚未解决。JSON:位置0的JSON中的意外令牌o
这是我的jQuery
$("body").on("click", "input.add", function(event) {
var myData = {"p":'Hello'};
var myJSON = JSON.stringify(myData);
$.ajax({
type:"POST",
url:'test2.php',
data:{'mydata':myJSON},
dataType:"json",
cache:false,
success: function(result){
$(".message").html('SUCCESS: '+result);
},
error: function(xhr, ajaxOptions , thrownError){
$(".message").html('ERROR: '+thrownError);
console.log("Data: "+thrownError);
}
});
event.preventDefault();
return false;
});
这是我的PHP代码:
header('Content-Type:application/json');
var_dump(json_decode($_POST['mydata']));
if(isset($_POST['mydata'])){
echo 'found';
}
else
echo 'Not found';
请帮我在哪里,我做错了,哪里是我的错误,我应该怎么改?
答
您正在返回字符串,并设置为JSON删除此行
dataType:"json",
而且从Ajax响应评论VAR转储
答
使用$_POST
的JSON类型的数据不能得到的,对于这一点,你必须USEL
$paramsStr = file_get_contents('php://input');
$params = json_decode($paramsStr, true);
+0
如果我使用此代码,然后我得到意想不到的令牌N在Json的位置0 –
+0
仍然这个问题是打开的? –
'stripslashes' mydata – guradio
做了任何ans为你工作? – C2486
@Rishi没有ans的工作尚未 –