Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery遍历WP-restapi-AJAX数组_Javascript_Jquery_Json_Wordpress_Wordpress Rest Api - Fatal编程技术网

Javascript 使用jQuery遍历WP-restapi-AJAX数组

Javascript 使用jQuery遍历WP-restapi-AJAX数组,javascript,jquery,json,wordpress,wordpress-rest-api,Javascript,Jquery,Json,Wordpress,Wordpress Rest Api,我试图循环使用从WP restapi创建的AJAX数组。从数组中的第一个帖子返回数据一点也不困难: $( '#button' ).on( 'click', function ( e ) { e.preventDefault(); $.ajax( { url: 'http://url.com/wp-json/posts?type=post&filter[posts_per_page]=20',

我试图循环使用从WP restapi创建的AJAX数组。从数组中的第一个帖子返回数据一点也不困难:

$( '#button' ).on( 'click', function ( e ) {
            e.preventDefault();
            $.ajax( {
              url: 'http://url.com/wp-json/posts?type=post&filter[posts_per_page]=20',
              success: function ( data ) {
                var post = data.shift(); // The data is an array of posts. Grab the first one.

                    $( '.item .front_feed_title' ).text( post.title );
                    $( '.item .front_feed_content' ).html( post.content );

              },
              cache: false
            } );
          } );
但是,我需要遍历AJAX数组,我无法找出适用于该数组的语法。我想将
数据
传递到
中。每个
函数都不起作用:

$( '#button' ).on( 'click', function ( e ) {
            e.preventDefault();
            $.ajax( {
              url: 'http://url.com/wp-json/posts?type=post&filter[posts_per_page]=20',
              success: function ( data ) {

                 $.each([data], function(i, objects) {

                     console.log(i.title);

                 });

              },
              cache: false
            } );
          } );

我的控制台显示该对象未定义。任何人都可以提供一些关于我应该如何将
数据
传递到jQuery
中的见解。每个
函数?

在不知道对象结构的情况下,很难说,但要尝试一下。 在函数中,“this”指的是当前对象。所以你可以引用像这样的元素

$.each(data,function(){

console.log( this.title ,this.content);

})

数据
是一个数组,因此传递该数组。当前,您正在将
数据
数组包装到另一个数组中,其中数据是其唯一的元素
[data]
。只需传递
数据
不要在不告诉我为什么传递
数据
而不是
[data]
在这种情况下似乎没有什么区别的情况下就否决投票。它仍然没有定义。当我在下面使用@bassxzero answer时,我确实看到了数据,但只看到了第一个结果。他的方法几乎有效,但没有循环通过响应。不幸的是,我无法发布响应,因为它包含客户端敏感数据。您应该在回答和代码中解释您正在更改的内容和原因。为什么投票被否决?如果你不解释对回答者没有帮助…(不管你是谁)。谢谢你的回答,我试过了,几乎奏效了。它显示第一个结果,但不循环AJAX响应。你知道为什么会这样吗?(我没有投你的反对票)@mbacon40这确实贯穿了他们所有人。如果只看到一个结果,那么api调用只返回一个结果。你有一个示例API URL吗?啊哈!我发现我有
var post=data.shift()在我运行.each之前。删除此选项修复了此问题。谢谢