继续收到此作为$ .ajax帖子的回报。
" 401(API请求需要HTTP Basic Auth或OAuth。) 对预检请求的响应没有通过访问控制检查:否'访问控制 - 允许 - 来源'标头出现在请求的资源上。"
使用postman或hurl.it返回200 OK。
我已经写下面的内容继续看下面的错误。我应该在url下而不是在beforeSend中添加标题,还是我省略了另外一个未知参数?
这是http://api.myemma.com/的ajax POST,使用的凭据和帐户ID有效。
var data = {
"fields": {
"first_name": "fname",
"last_name": "lname",
"postal_code": "00000"
},
"email": "test@testing.com",
},
username = "username",
password = "password",
accountid = "0000000";
$.ajax({
type: "POST",
url: "https://api.e2ma.net/"+accountid+"/members/add",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
xhrFields: {
withCredentials: true
},
beforeSend:function(xhr){
console.log("beforesend");
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
success:function(data){
alert("success");
},
error:function(error){
console.log(error);
}
});