【发布时间】:2019-06-21 13:43:09
【问题描述】:
我正在尝试使用 API 根据用户 ID 获取有关特定用户的信息。我需要使用基本身份验证并在我的调用中传递一些标头。 我得到了这个:
Cross-Origin Read Blocking (CORB) blocked cross-origin response "rest/user/getProfile?callback=jQuery224033348109431646855_1548684613983&userId=24068..." with MIME type text/plain.
我的代码:
$.ajax
({
type: 'GET',
crossDomain: true,
async: false,
url: 'example_URL_/api/user/getProfile',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", "Basic ZHVubmVzYXBpskjoi43u5409543o9tI654kjhugjy"); },
dataType: 'jsonp',
data: { "Id": "1234" },
success: function (data) {
console.log(data);
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
}
});
我有什么遗漏吗?
【问题讨论】:
-
您不能从 javascript 代码中设置 CORS。
-
是的,那是我的测试,谢谢...
-
您遇到的问题是尝试进行 JSONP 调用并尝试设置标头。 JSONP 调用使用脚本标记,而不是 XMLHttpRequest
标签: javascript ajax rest api get