getJSON 回调未发生
我在这里查看了类似的查询,但我似乎无法将它们应用于我的问题。我对 jquery 还很陌生,所以我可能做了一些愚蠢的事情。 我有一个简单的 getJSON 测试:
$(document).ready(function() {
$(".testurl").click(function() {
// do a json call
var url="testjson.php";
var rc=$.getJSON(
url,
{parm1: "P1", parm2: "P2", parm3: "P3"},
function(data){
alert("callback function inline");
});
var x = 1;
});
});
调用一个非常简单的脚本:
header("Content-Type: application/json");
echo "{\"results\": [";
$arr = array();
$arr[] = "{\"fld1\": \"result1\", \"fld2\": \"result2\", \"fld3\": \"result3\"}";
echo implode(", ", $arr);
echo "]}";
返回有效 JSON(我检查了 JSON lint)
我用来接收请求结果的 var rc 具有以下值:
getResponseText \"{\"results\": [{\"fld1\": \"result1\", \"fld2\": \"result2\", \"fld3\": \"result3\"}]}\""
getReadyState 4
getStatus 200
为什么回调函数没有触发?
I have looked at similar queries here, but I can't seem to apply them to my problem. I am pretty new to jquery, so I may be doing something dumb.
I have a simple getJSON test:
$(document).ready(function() {
$(".testurl").click(function() {
// do a json call
var url="testjson.php";
var rc=$.getJSON(
url,
{parm1: "P1", parm2: "P2", parm3: "P3"},
function(data){
alert("callback function inline");
});
var x = 1;
});
});
that calls a really simple script:
header("Content-Type: application/json");
echo "{\"results\": [";
$arr = array();
$arr[] = "{\"fld1\": \"result1\", \"fld2\": \"result2\", \"fld3\": \"result3\"}";
echo implode(", ", $arr);
echo "]}";
that returns valid JSON (I checked on JSON lint)
The var rc that I use to receive the result of the request has the following values:
getResponseText \"{\"results\": [{\"fld1\": \"result1\", \"fld2\": \"result2\", \"fld3\": \"result3\"}]}\""
getReadyState 4
getStatus 200
Why does the callback function not fire?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果单击处理程序附加到锚元素,则 ajax 调用可能没有时间在重定向到其他页面之前执行。尝试将
return false
添加到点击回调:If the click handler is attached to an anchor element then maybe the ajax call doesn't have time to execute before redirecting to a different page. Try adding a
return false
to the click callback:$.getJSON 调用只是分配给一个变量,它没有被调用。
The $.getJSON call is just assigned to a variable, it is not being called.