简体   繁体   English

jQuery AJAX类型jsonp,返回格式错误的JSON吗?

[英]jQuery AJAX type jsonp, returning malformed JSON?

$('#rn_s').keyup(function() {
    var rn = $('#rn_s').val();

    if(rn.length == 9) {
        $.ajax({
            url: 'http://routingnumbers.info/api/data.json?rn=' + rn,
            type: 'GET',
            dataType: 'jsonp', 
            success: function(result) {
                console.log(result);
            }
        });
    }
});

And it returns this: 它返回此:

Resource interpreted as Script but transferred with MIME type text/plain

And then returned content is Object {} 然后返回的内容是Object {}

How can I access these values? 如何访问这些值?

Seems to be working okay for me. 似乎对我来说还可以。 I created a fiddle, using chrome and the latest version of jQuery (not v2). 我使用chrome和最新版本的jQuery(不是v2)创建了一个小提琴。

This is basically what you had, minus the keyUp events: 基本上是您所拥有的,减去keyUp事件:

var rn = 122242597;

$.ajax({
    url: 'http://routingnumbers.info/api/data.json?rn=' + rn,
    type: 'GET',
    dataType: 'jsonp',
    success: function (result) {
        console.log(result);
        $('#customerName').text(result.customer_name);
        $('#address').text(result.address);
        $('#zipCode').text(result.zip);
    } });

Then in your HTML: 然后在您的HTML中:

<div>
    <span id="customerName"></span>
</div>
<div>
    <span id="address"></span>
</div>
<div>
    <span id="zipCode"></span>
</div>

Check this fiddle for a working example. 查看此小提琴以获取可行的示例。 Obviously your HTML will differ, but accessing the returned properties and assigning them to your UI should be similar. 显然,您的HTML会有所不同,但是访问返回的属性并将其分配给UI的方式应该相似。

Have you looked at that API's documentation? 您是否看过该API的文档? That call returns a json object. 该调用返回一个json对象。

http://www.routingnumbers.info/api/data.html http://www.routingnumbers.info/api/data.html

To access the values, look at the API documentation for the value names. 要访问这些值,请参阅API文档中的值名称。 For example, to get the address of the result: 例如,要获取结果的地址:

var address = result.address

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM