$(document).ready(function() { $.ajax({ type: "GET", dataType: "json", contentType: "text/plain", xhrFields: { withCredentials: true }, url: 'http://kitabisa.xyz/api/v2/campaigns', username: 'apps1', password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR', success: function(result) { alert('done'); }, complete: 'callback' }) });
我想通过jquery Ajax函数从API获取JSON,上面是我的代码片段,但是CORS Policy阻止了ajax,这里是控制台消息。
I've tried the another method, I use beforeSend ajax here :
$(document).ready(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "text/plain",
xhrFields: {
withCredentials: true
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic YXBwczE6WEQ2V1ZqaGNxM0pWckZSbGRycGpFcEF5VVhnNUxGelM5Y21pR3VYTDNUbUU3YmtMR1I=")
},
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR',
success: function(result) {
alert('done');
}
})
});
但仍然行不通。
有什么建议可以解决这个问题吗?谢谢
答案 0 :(得分:0)
尝试以下
$.ajax({
type: "GET",
dataType: "json",
contentType: "text/plain",
xhrFields: {
withCredentials: true,
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR'
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
success: function(result) {
alert('done');
},
});