简体   繁体   English

Uncaught TypeError:在jQuery ajax调用中解析JSONP响应时,number不是函数

[英]Uncaught TypeError: number is not a function when parsing JSONP response in jQuery ajax call

I am trying to grab data from a wordpress blog using the WP-API plugin. 我正在尝试使用WP-API插件从wordpress博客中获取数据。 My js file is using jQuery to make ajax calls to the api. 我的js文件正在使用jQuery对API进行Ajax调用。 I need to use JSONP as the response type since I am trying to access cross domain information. 由于我要访问跨域信息,因此我需要使用JSONP作为响应类型。

When the page loads, I get an error "Uncaught TypeError: number is not a function" and my response begins like this: /**/1([{"ID":231,"title":blahblahblah... with the error pointing at the "1" in the console. 页面加载后,出现错误“未捕获的TypeError:数字不是函数”,并且我的响应如下所示: /**/1([{"ID":231,"title":blahblahblah...与错误指向控制台中的“ 1”。

This is the code I am using to try and grab the data and parse it: 这是我用来尝试获取并解析数据的代码:

function get_posts(num_offset) {
        offset = num_offset,
        url = 'http://www.example.com/wp-json/posts?_jsonp=1&filter[posts_per_page]=5&filter[offset]=' + offset;

        $.ajax({
            type: 'GET',
            dataType: "jsonp",
            url: url,
            success: function (data) {
                consol.log(data);
                console.log(url);
            }
        });

        // second ajax call to get the total number of posts
        $.ajax({
            type: 'GET',
            dataType: "jsonp",
            url: 'http://www.example.com/wp-json/posts?_jsonp=1&filter[offset]=-1',
            success: function (data) {
                console.log(data);
            }
        });


    }

I guess I need to know how I can remove the "Uncaught TypeError: number is not a function" error and then how I would be able to parse the JSONP data. 我想我需要知道如何删除“ Uncaught TypeError:number is not a function”错误,然后如何解析JSONP数据。

much thanks! 非常感谢!

You're telling the other end that your callback's name is 1 , in your URL: 您要告诉另一端,您的URL中的回调名称为1

http://www.example.com/wp-json/posts?_jsonp=1&filter[offset]=-1
Here --------------------------------^^^^^^

You want to use the name of the callback function you have on your page that you're expecting to receive the JSONP response, which needs to be a validate identifier literal (so not 1 ). 您要使用您希望接收JSONP响应的页面上具有的回调函数的名称,该名称必须是有效的标识符文字(而不是1 )。

Because you're using jQuery, it's best by far if you let jQuery worry about what the callback name should be, by leaving off that query parameter entirely and letting jQuery add it. 因为您使用的是jQuery,所以到目前为止,最好是让jQuery完全取消该查询参数,然后让jQuery添加它,以使它担心回调名称应为什么。 Since _jsonp is an unusual query string parameter name for this, you'll need to tell jQuery what the parameter name is. 由于_jsonp是一个不寻常的查询字符串参数名称,因此,您需要告诉jQuery参数名称是什么。

$.ajax({
    type: 'GET',
    dataType: "jsonp",
    url: 'http://www.example.com/wp-json/posts?&filter[offset]=-1',
    // No _jsonp=1 here ------------------------^
    jsonp: '_jsonp',
    // ^^ new parameter here
    success: function (data) {
       console.log(data);
    }
});

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

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