我是.Net开发的新手,我想打电话给客户端服务器。当我用POSTMAN运行代码行时,它运行得很好。但是当我在java脚本中使用相同的代码/头文件时,我能够获得所需的结果。
以下是我正在使用的代码行
var xhr = new XMLHttpRequest();
var params = "grant_type=password";
xhr.open("POST", "ClientURL");
xhr.setRequestHeader("Authorization", "Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Data-Type", "json");
xhr.setRequestHeader("Host", "URL");
xhr.send(params);
console.log(xhr.status);
console.log(xhr.statusText)
或者
$.ajax({
type: "POST",
url: 'CLIENT URL',
dataType: "json",
headers: { 'Authorization': 'Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=' },
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic "ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj="');
},
success: function (data) {
if (data) {
alert(data);
}
else {
alert("Something went wrong");
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
通话中是否有任何遗漏?任何帮助表示赞赏
下面是IE中的错误
XMLHttpRequest: Network Error 0x80070005, Access is denied.
并在Chrome中
Default.aspx:68 Uncaught SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=' is not a valid HTTP header field value.
答案 0 :(得分:0)
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic ZDJWQ1NmE4NjA4MDc0MjQ2NzSfdsdakj=");
},
按上述方式更改功能。