JavaScript、Ajax、php解析器错误,以及如何获取post Json?
我在game.php中得到了刷新.refre的代码,但我总是得到“parsererror”。jsonString来自jsonString=JSON.stringify(对象)JavaScript、Ajax、php解析器错误,以及如何获取post Json?,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我在game.php中得到了刷新.refre的代码,但我总是得到“parsererror”。jsonString来自jsonString=JSON.stringify(对象) 。。 ... jsonString={“right”:0,“arrayQ”:[{“0”:“10”,“idQ”:“19”}, {“0”:“34”,“idQ”:“20”},{“0”:“89”,“idQ”:“14”}],“生活”:40}; $.ajax({ 键入:“POST”, 资料来源:jsonString, 数据类型:“js
。。
...
jsonString={“right”:0,“arrayQ”:[{“0”:“10”,“idQ”:“19”},
{“0”:“34”,“idQ”:“20”},{“0”:“89”,“idQ”:“14”}],“生活”:40};
$.ajax({
键入:“POST”,
资料来源:jsonString,
数据类型:“json”,
url:'Q.php',
contentType:'application/json;charset=utf-8'
}).完成(功能(数据、状态){
$('.refre').load('Q.php');
警惕(“右”);
})
.失败(功能(数据、状态){
警报(“错误:+状态”);
});
...
另一个文件是Q.php,它读取文章并发送一些HTML,现在它只是为了检查文章的信息
<?php
$value = json_decode($_POST);
$categories = json_decode(file_get_contents('php://input'));
print_r($value);
print_r($categories);
?>
ajax有什么问题??如何在Q.php中获得帖子?如何从Q.php中的JSON中获得“生命”?试试这个
<div class="refre"></div>..
<script>...
jsonString =[{"right":0,"arrayQ":[{"0":"10","idQ":"19"},
{"0":"34","idQ":"20"},{"0":"89","idQ":"14"}],"lives":40}];
$.ajax({
type: 'POST',
data: jsonString,
dataType: 'html',
url: 'Q.php',
contentType : 'application/json; charset=utf-8'
}).done( function (data, status) {
$('.refre').html(data);
alert('Right');
})
.fail( function (data, status) {
alert("Wrong: "+status);
});
...</script>
。。
...
jsonString=[{“right”:0,“arrayQ”:[{“0”:“10”,“idQ”:“19”},
{“0”:“34”,“idQ”:“20”},{“0”:“89”,“idQ”:“14”}],“生活”:40}];
$.ajax({
键入:“POST”,
资料来源:jsonString,
数据类型:“html”,
url:'Q.php',
contentType:'application/json;charset=utf-8'
}).完成(功能(数据、状态){
$('.refre').html(数据);
警惕(“右”);
})
.失败(功能(数据、状态){
警报(“错误:+状态”);
});
...
您所谓的
jsonString
不是JSON,它是一个javscript对象
当您将一个对象传递给$.ajax
的数据时,jQuery将对该数据进行编码
在php中,您可以使用$\u POST['lifes']
检索生活它不是作为JSON发送的
将对象中的键视为表单输入的name
至于输出,您只能从服务器返回一个JSON字符串。JSON必须有一组打开/关闭大括号,以便可以将其转换为一个数组或对象
<div class="refre"></div>
<script>
jsonString =[{"right":0,"arrayQ":[{"0":"10","idQ":"19"},{"0":"34","idQ":"20"},{"0":"89","idQ":"14"}],"lives":40}];
$.ajax({
type: 'POST',
data: jsonString,
url: 'Q.php',
}).done( function (data, status) {
$('.refre').html(data);
alert('Right');
})
.fail( function (data, status) {
alert("Wrong: "+status);
});
</script>
这没关系。“数据”不会作为json发送$_POST是Q.php中的一个数组,您不需要对其进行json解码。使用broswer的开发者工具->网络选项卡的网络选项卡查看ajax请求返回的值。如果您将数据类型:“json”、
替换为数据类型:“html”、
,数据将作为json发送吗?我的意思是,这种变化是否意味着我们失去了JSON的优势?
<?php
$value = json_decode($_POST);
$categories = json_decode(file_get_contents('php://input'));
print_r($value);
print_r($categories);
?>
<div class="refre"></div>
<script>
jsonString =[{"right":0,"arrayQ":[{"0":"10","idQ":"19"},{"0":"34","idQ":"20"},{"0":"89","idQ":"14"}],"lives":40}];
$.ajax({
type: 'POST',
data: jsonString,
url: 'Q.php',
}).done( function (data, status) {
$('.refre').html(data);
alert('Right');
})
.fail( function (data, status) {
alert("Wrong: "+status);
});
</script>
print_r($_POST);