javascript
  • ajax
  • jsonp
  • 2017-11-11 155 views 0 likes 
    0

    請幫忙從遠程服務器獲取JSONP數據:如何從遠程服務器獲取jsonp?

    document.addEventListener("DOMContentLoaded", function() { 
        function readresponse(response){ 
         console.log(response); 
        } 
        (function(){ 
         var src = 'http://json-schema.org/draft-04/schema#?callback=readresponse'; 
         var script = document.createElement('SCRIPT'); 
         script.src = src; 
         document.body.appendChild(script); 
        })(); 
    }); 
    

    而Chrome瀏覽器選項卡「網絡」顯示200點的狀態和正確的JSON響應

    回答

    1

    如果你看看你的src網址傳回的內容你會看到它是一個JSON而不是JSONP。如果它是JSONP你的​​src應該是:

    var src = 'http://json-schema.org/draft-04/schema&callback=readresponse' 
    

    ,它會返回數據將被包裝爲:

    readresponse({...}) 
    

    ,而不是僅僅

    {...} 
    

    這就是爲什麼你收到解析錯誤。


    您可以在此主題閱讀this post瞭解更多信息。

    相關問題