我使用不同的服务器上的API,我有一个CORS错误,我认为。奇怪的是,它首先没有问题的工作,然后我得到这个错误信息没有'访问控制允许来源'标题出现在与json和jquery请求的资源错误
XMLHttpRequest cannot load http://www.thecocktaildb.com/api/json/v1/1/random.php? tagmode=any&type=POST&format=jsonp. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'myadress.com' is therefore not allowed acces
我加入crossDomain: "true"
和它的工作了一天。现在它不再工作,我已经搜索并尝试了很多我找到的解决方案。但没有任何工作。什么是问题,我如何解决它?试过jsonp
而不是json
有没有和& callback =?什么也没做。我甚至为chrome安装了CORS启用扩展。但同样的错误,我无法控制API本身或托管它的服务器。我怎样才能解决这个问题?以下是我的代码。
function random() {
$(document).ready(function() {
$.getJSON("http://www.thecocktaildb.com/api/json/v1/1/random.php", {
tagmode: "any",
type: "POST",
format: 'jsonp',
crossDomain: "true"
}, function (data) {
console.log(data);
var result = "";
$.each(data.drinks, function (index, value) {
result += "<p>" + value.idDrink + "<p>";
result += "<p>" + value.strDrink + "<p>";
});
$('#result').html(result);
console.log(result);
});
});
}
该网站没有设置标题,所以没有什么可以从您的结尾做。 Severside代理是你需要的。 – epascarello
服务器不返回jsonp但是json。然后,您应该使用您自己的服务器来提供请求 –