.ajax 错误 - a 未定义 a))();else c.error(“无效的 JSON:

发布于 2024-09-28 15:12:17 字数 1892 浏览 5 评论 0原文

在下面的代码中,为什么它有效:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2024-10-05 15:12:17
jQuery.parseJSON(response);

$.each 之前尝试这个,因为 Jquery 认为你的答案是一个字符串,而不是一个 JSON 对象。

jQuery.parseJSON(response);

Try this before $.each, cause Jquery thinks that your answer is a string, not a JSON-object.

梦过后 2024-10-05 15:12:17

看起来您没有从服务器返回有效的 JSON,而不是 addresses JSON 无效(尽管它只是一个对象文字)。您确定这是正确的网址吗?

url: ';addresses?company=' + ui.item.id

检查 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?

url: ';addresses?company=' + ui.item.id

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文