以下是使ajax调用有问题的函数。以下是插入的参数,如Firefox中的javascript调试器所示:
JSON.stringify(c) = {"start":0,"length":20,"sortBy":"dc","sortAscending":false,"providerId":128391,"currencyIds":[],"fromDateStr":"2007-01-01","toDateStr":"2014-01-10","validTrades":true,"lotSize":2}
b.resolveUrl(e) = "http://localhost/j1/test/proxy.php"
g = [object Window]
b = [object Object]
a.isFunction(d) && d.call(g, c, f, g) = false
功能:
a.ajax(a.extend({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(c),
url: b.resolveUrl(e),
context: g,
cache: !1,
success: function (c, e, d) {
a.isFunction(f) &&
f.call(g, "undefined" != typeof c.d ? b.parseJSON(c.d) : c, g, e, d)
},
error: function (b, c, f) {
a.isFunction(d) && d.call(g, c, f, g)
}
}, h))
为什么会出错?怎么了?我在proxy.php文件的开头插入了以下内容,以确保数据正确发布:
if(empty($_POST)) die("POST failed!");
在请求的响应中,我看到“POST失败!”消息。
答案 0 :(得分:0)
看起来您要发布json
而不是表单编码数据。这意味着PHP无法提取数据并将其放入$_POST
,因此将为空。
如果您的数据发送正确,您应该能够从http_get_request_body()
获取数据并使用json_decode()
对其进行解码(如果您想要关联数组而不是对象,请将第二个参数设置为true)
如果您想在$_POST
中接收数据,请不要将JSON.stringify
应用于客户端的数据。 jQuery非常乐意对表单进行编码。