.ajax 错误 - a 未定义 a))();else c.error(“无效的 JSON:
在下面的代码中,为什么它有效:
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
但
var addresses = json.val;
不起作用
我的 json 输出是有效的!
{"2-avenue-bir-hakiem": "2 比尔大道 哈基姆", "56-rue-marcel-pagnol": "56 马塞尔·帕尼奥尔街”}
我得到的错误是
a 未定义 [中断此错误] a))();else c.error("无效的 JSON: “+a)...f(d)if(i)for(f in a){if(b.apply(a[f],
<script>
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#companies" ).autocomplete({
source: ";companies",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
$("#address").html(ui.item.id);
$.ajax({
type: 'GET',
url: ';addresses?company=' + ui.item.id,
dataType: 'json',
// process the addresses
success: function(json) {
$('body').append('Response Value: ' + json.val);
var opts = '';
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
//var addresses = json.val;
$.each(addresses, function(k, v) {
opts += '<option>' + v + '</option>';
});
$('#address').html(opts);
}
}); //end ajax
} // end select
});
});
</script>
我错过了什么?
谢谢
in the following code, why does it work:
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
but
var addresses = json.val;
does not work
my json output is valid!
{"2-avenue-bir-hakiem": "2 Avenue Bir
Hakiem", "56-rue-marcel-pagnol": "56
rue Marcel Pagnol"}
the error i get is
a is undefined [Break on this error]
a))();else c.error("Invalid JSON:
"+a)...f(d)if(i)for(f in
a){if(b.apply(a[f],
<script>
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#companies" ).autocomplete({
source: ";companies",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
$("#address").html(ui.item.id);
$.ajax({
type: 'GET',
url: ';addresses?company=' + ui.item.id,
dataType: 'json',
// process the addresses
success: function(json) {
$('body').append('Response Value: ' + json.val);
var opts = '';
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
//var addresses = json.val;
$.each(addresses, function(k, v) {
opts += '<option>' + v + '</option>';
});
$('#address').html(opts);
}
}); //end ajax
} // end select
});
});
</script>
what am i missing?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
$.each
之前尝试这个,因为 Jquery 认为你的答案是一个字符串,而不是一个 JSON 对象。Try this before
$.each
, cause Jquery thinks that your answer is a string, not a JSON-object.看起来您没有从服务器返回有效的 JSON,而不是
addresses
JSON 无效(尽管它只是一个对象文字)。您确定这是正确的网址吗?检查 firebug、chrome、fiddler 中返回的响应...无论您可以使用什么来查看该响应,并在此处检查它是否有效:http://www.jsonlint.com/
从您的症状来看,您目前收到的回复完全是空的,我认为您使用的网址是要归咎于,请仔细检查您要获取的内容。
It looks like you're not getting valid JSON back from the server, not that the
addresses
JSON is invalid (though it's just an object literal). Are you sure this is the right URL?Check the response coming back in firebug, chrome, fiddler...whatever you can use to see that response, and check that it's valid here: http://www.jsonlint.com/
It appears from your symptoms that you're currently getting a completely empty response back, and I think the URL you're using is to blame, double check what you're trying to fetch.