【发布时间】:2014-05-20 21:09:31
【问题描述】:
这是我的 JSON 函数:
function createjson(){
var json='{"jsondata":[[';
$("#kup td").each(function(){
if($(this).html()!="")
{
json+= parseInt($(this).html())+",";
if($(this).index()%5==0 && $(this).index()!=0){
json=json.substring(0,json.length-1);
json+="],["
}
}
});
json=json.substring(0,json.length-3);
json+="]]}";
console.log(json); //-> works fine.
return json;
};
AJAX部分:
$("#button").click(function(){
$.ajax({
type:"POST",
url:"x.php",
data: createjson(),
contentType: "application/json",
dataType: "json",
success: function(result) {
alert("done"); //->works
}
});
});
PHP部分:
<?php
header('Content-type: application/json');
echo "<pre>";
echo $_POST['jsondata'];
echo "</pre>";
?>
因此,“警报”有效,但在控制台中检查响应时,它仅返回“<pre></pre>”
有什么办法吗?
【问题讨论】:
-
json 数据不能包含
元素
-
你有没有使用你的开发者工具来检查你的 JS 向服务器发出的请求?
jsondata密钥发送了什么?此外,如果您使用要发送到服务器的键/值对创建对象,则无需自己创建 JSON 字符串,jQuery 将为您进行参数化。 -
还检查您发送的 json 的有效性,例如使用 jsonlint
-
@Jasper
console.log(json);部分工作正常。 json 验证器没有给出错误。我把这个寄给我:{"jsondata":[[10,13,18,27,37,48]]} -
@WhoCares 在 PHP 中做一个
var_dump($_POST['jsondata']);怎么样?