我想提出几个Ajax请求到纽约时报国会API ,而迭代区的阵列(1〜5)。 我只能够在平均成功返回2个请求,剩下的返回:
XMLHttpRequest cannot load https://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/house/OR/2/current.json?api-key=xxxxxxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9001' is therefore not allowed access.
看来,没有模式这些缺陷。 即使使用相同的_district
数倍次失败,看似随意。
我的代码:
var title_element,
title_render,
district_numbers = ['1', '2', '3', '4', '5'],
element_ids = ['ushd1', 'ushd2', 'ushd3', 'ushd4', 'ushd5'],
apiKey = 'api-key=xxxxxxxxxxx',
baseUrl = 'https://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/house/OR/',
orUsHouseMembers,
frag;
for (var i = 0, total_districts = district_numbers.length; i < total_districts; i++) {
(function (i) {
var myUrl = baseUrl + district_numbers[i] + '/current.json?' + apiKey;
$.ajax({
url: myUrl,
dataType: 'json',
success: function (data) {
console.log('\nSUCCESS: ' + myUrl);
title_element = document.getElementById(element_ids[i]);
title_render = document.createTextNode(data.results[0].name + ' - District ' + data.results[0].district );
title_element.appendChild(title_render);
},
error: function (data) {
console.log('\nFAIL: ' + myUrl);
}
})
})(i);
}
我尝试添加Access-Control-Allow-Origin: '*'
和所有常见的CORS的东西,但我不希望这是一个真正的CORS问题,因为失败过间歇性
东西在我的代码不妥或可能的API?