Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Jquery 如何通过单击按钮访问API响应_Jquery_Ajax_Wordpress Rest Api - Fatal编程技术网

Jquery 如何通过单击按钮访问API响应

Jquery 如何通过单击按钮访问API响应,jquery,ajax,wordpress-rest-api,Jquery,Ajax,Wordpress Rest Api,我需要访问一个公共引用JSONAPI并在HTML页面中显示内容,我对jQueryAjax方法知之甚少。但是我的代码无法将数据拉入HTML。请用我的代码解释这个错误 $(".btn-primary").on("click",function(){ $.ajax({ type:"GET", url:"http://quotesondesign.com/wp-json/posts? filter[orderby]=rand", succes

我需要访问一个公共引用JSONAPI并在HTML页面中显示内容,我对jQueryAjax方法知之甚少。但是我的代码无法将数据拉入HTML。请用我的代码解释这个错误

$(".btn-primary").on("click",function(){
    $.ajax({
       type:"GET",
       url:"http://quotesondesign.com/wp-json/posts?
      filter[orderby]=rand",
       success:function(data){
        var post = data.shift(); 
        $("#quote").html(post.content);
      }
   });
});
试试这个:

window.onload = function() {
    $(".btn-primary").on("click",function(){ 
        $.ajax({ 
            type:"GET", 
            url:"https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", 
            success:function(data){ 
                $("#quote").append(data[0].content)
            },
            cache: false
        }); 
    });
}
如果您是从安全站点运行此服务,您的ajax url将需要启动wi“https”,否则,“http”将起作用


此外,您还需要“cache:false”部分,否则它将继续显示相同的引号。

使用浏览器上的开发人员模式在控制台中是否可以看到任何错误?你也可以显示HTML代码吗?你可以发布你的HTML吗?至少在你有报价和按钮的地方。我看不到您的代码中有任何错误。只是在本地测试了一下,效果很好。