检索和显示jQuery GET请求的结果

Retrieve and display results of jQuery GET request

本文关键字:请求 结果 GET jQuery 显示 检索      更新时间:2023-09-26

我试图从ESV API检索和显示数据:http://www.esvapi.org/
它正在codecademy.com域名上工作,而不是esvapi.org域名。
参见:http://jsfiddle.net/BinaryAcid/yqCcn/

<input type="button" value="get data" id="btn" >
$("#btn").click(function() {
    var response = '';
    $.ajax({ type: "GET",   
        // url: "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=John+1",
        url: "http://www.codecademy.com/",
        async: false,
        success : function(text)
        {
         response = text;
        }
    });
    document.write(response);
});

我尝试了fiddle的例子,但没有与第一个或第二个URL的工作,问题是有关的跨域调用,你不能直接调用一个服务,不是在你自己的域,除非你使用json或设置一些头在你的服务器明确允许跨域调用(这种技术不会在IE中工作)

可以。参见jsfiddle: http://jsfiddle.net/BinaryAcid/qDrw8/1/

<input type="button" value="get data" id="btn" >
$("#btn").click(function() {
reference='Jhon+1'
$.getJSON('http://www.esvapi.org/crossref/ref.php?reference=' + reference + '&callback=?',
    function(text){
        if(text){
            $('body').html(text.content);
        } else {
            $('body').html('Error');
        }
    });
});

雅虎查询语言(YQL)解决方案。参见jsfiddle: http://jsfiddle.net/BinaryAcid/jbCuH/1/

<input type="button" value="get data" id="btn">
$("#btn").click(function () {
var response = '';
var url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=John+1";
var yql = "select content from data.headers where url='" + url + "'";
$.ajax({
    type: "GET",
    url: "http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(yql) + "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=?",
    async: false,
    dataType: "json",
    success: function (data) {
        response = data.query.results.resources.content;
        document.write(response);
    }
});
});