gpt4 book ai didi

javascript - jQueries .getJSON 命令上跳过回调函数

转载 作者:行者123 更新时间:2023-11-28 16:36:39 26 4
gpt4 key购买 nike

我有以下代码片段:

$('input#teamName').live('blur', function() {
var value = $(this).val();
if (value) {
$.getJSON('api/event_company/'+value, function(data) {
console.log('why does this not want to work?');
});
}
});

基本上,它所做的就是在表单字段更改时从服务器请求一些数据。我的问题是,即使我可以使用 firebug 看到它已成功向服务器发送请求并收到有效的 JSON 响应,但回调函数中的任何内容都没有被调用。

如果我将 getJSON 参数更改为:

$.getJSON('api/event_company/'+value, alert('Blah'));

然后警报就会按预期弹出。有什么想法可能导致这种行为吗?

最佳答案

如果 JSON 无效,解析将失败并且不会调用处理程序。来自 getJSON文档:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

看看你的 JSON 是否 validates .

你的第二个例子不正确。应该是,

$.getJSON('api/event_company/'+value, function() {
alert('Blah');
});

关于javascript - jQueries .getJSON 命令上跳过回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3669685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com