2013-04-09 10 views
0

我試圖從ESV API檢索和顯示數據:http://www.esvapi.org/
它正在處理codecademy.com域而不是esvapi.org域。
見琴:http://jsfiddle.net/BinaryAcid/yqCcn/檢索並顯示jQuery GET請求的結果

<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); 

}); 
+1

嘗試添加以下到您的Ajax調用:數據類型:「JSONP」 – Lowkase 2013-04-09 17:45:23

+0

也許你需要憑據或只是調整標題.. – fernandosavio 2013-04-09 17:46:10

+0

你不能在頁面加載後使用文件撰寫。它會做一些討厭的事情來替代所有的頁面內容。如果您正在調試,請使用控制檯。 – epascarello 2013-04-09 17:49:51

回答

0

This works。見的jsfiddle:使用雅虎查詢語言(YQL)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'); 
     } 
    }); 
}); 
0

我試着撥弄例子,但並沒有與第一或第二的URL,這個問題是關係到跨域調用工作,你不能直接打電話到這是一個服務不在你自己的域名上,除非你使用jsonp或者在你的服務器中設置一些明確允許跨域調用的頭文件(這種技術在IE中不會工作)

0

解決方案。見的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); 
    } 
}); 

}); 
相關問題