Javascript 如何处理外部api JSON而不是JSONP

Javascript 如何处理外部api JSON而不是JSONP,javascript,jquery,ajax,json,api,Javascript,Jquery,Ajax,Json,Api,我试图调用,如果直接转到api url(?),它将返回看起来不错的json { currentPage: 1, numberOfPages: 1, totalResults: 4, data: [], status: "success" } 当我通过jQuery运行json请求时 $.getJSON("http://api.brewerydb.com/v2/search/geo/point?key=MYKEYHERE& lat=46.8677308&lng=-96.810831

我试图调用,如果直接转到api url(?),它将返回看起来不错的json

{
currentPage: 1,
numberOfPages: 1,
totalResults: 4,
data: [],
status: "success"
}
当我通过jQuery运行json请求时

$.getJSON("http://api.brewerydb.com/v2/search/geo/point?key=MYKEYHERE&
lat=46.8677308&lng=-96.81083149999999&callback=?", function(){console.log("herp my derp");})
它没有成功执行,并且我打印到控制台的文本也没有执行。在我得到

enter code here SyntaxError: missing ; before statement
如果删除回调参数,就会得到跨站点脚本小调

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 
http://api.brewerydb.com/v2/search/geo/point?key=MYKEYHERE&lat=46.867741699999996&lng=-96.811004. 
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
我最初尝试使用jsonp,但它们似乎没有提供这种格式的响应。耶

如何仅使用jQuery/Javascript处理此JSON

如何仅使用jQuery/Javascript处理此JSON


他们需要添加CORS头或JSONP响应。只有jQuery/JavaScript,您在这方面做不到任何事情。

为了避免沙箱限制,请尝试通过您的服务器代理他们的服务

下面是一个使用PHP的示例

你的JS:

$.getJSON("/brewery.php?lat=46.8677308&lng=-96.81083149999999", function(){console.log("herp my derp");})
brewery.php:

<?php
echo file_get_contents("http://api.brewerydb.com/v2/search/geo/point?key=YOURKEYHERE&lat={$_GET['lat']}&lng={$_GET['lng']}&callback=?");