2016-12-25 117 views
0

我試圖使用.ajax和.getJSON,但都沒有工作。我正在使用goodzer休息API。REST API調用不起作用

代碼:

$(document).ready(function(){ 
    $("#click").click(function() { 
    $.ajax({url: "https://api.goodzer.com/products/v0.1/search_stores/?query=v-neck+sweater&lat=40.714353&lng=-74.005973&radius=5&priceRange=30:120&apiKey=APIKEY", success: function(result){ 
     console.log(JSON.stringify(result)); 
       }}); 
     }); 
    }); 
$.ajax({ 
    url: 'https://api.goodzer.com/products/v0.1/search_stores/?query=v-neck+sweater&lat=40.714353&lng=-74.005973&radius=5&priceRange=30:120&apiKey=APIKEY', 
    type: 'GET', 
    success: function() { alert('get completed'); } 
}); 
+2

您應該儘快刪除該密鑰。這個網站被谷歌索引,有人很可能會濫用您的密鑰。 – negacao

+0

你有什麼錯誤?你在瀏覽器上檢查過JavaScript控制檯嗎? –

+0

也許你可以添加你正在收到的修改輸出作爲迴應。 –

回答

0

我不得不代碼切換到

$(document).ready(function(){ 
    $("#click").click(function() { 
    $.getJSON("https://api.goodzer.com/products/v0.1/search_stores/?query=v-neck+sweater&lat=40.714353&lng=-74.005973&radius=5&priceRange=30:120&apiKey=API-KEY", function(result){ 
     alert(result.stores_found); 
       }); 
     }); 
    }); 

而且codePen由於某種原因,沒有讓這個代碼運行。

+0

很酷,我認爲問題出在你以前的代碼中,你得到的答案是HTML。 當你使用getJSON時,它發送這個頭Accept:application/json,它基本上告訴服務器客戶期望在json中的響應。 – Codeformer