我尝试了一切,我仍然无法弄清楚问题是什么,我试图发布' POST'在教师服务器上我们做家庭作业的东西,我不知道我做错了什么,我已经坚持了2个小时。
这是我的代码。
const xhr = new XMLHttpRequest();
const url = 'https://3uc5taw99i.execute-api.us-east-1.amazonaws.com/latest/attendees';
let attendee = JSON.stringify({firstName: 'Nikola',lastName:'Nikola',email:'Nikola',dateBirth:'13.12.1869'});
xhr.responseType = 'json';
xhr.onreadystatechange = function(){
if(xhr.readyState === XMLHttpRequest.DONE){
console.log(xhr.response);
}
}
xhr.open('POST',url);
xhr.send(attendee);
答案 0 :(得分:0)
请参阅此代码段,您将获得The 401 Unauthorized error
,您必须将授权令牌变量附加到请求中。
xhr.setRequestHeader('Authorization', '~Token~');
将会有另一个api将获得授权令牌。
const xhr = new XMLHttpRequest();
const url = 'https://3uc5taw99i.execute-api.us-east-1.amazonaws.com/latest/attendees';
let attendee = JSON.stringify({firstName: 'Nikola',lastName:'Nikola',email:'Nikola',dateBirth:'13.12.1869'});
xhr.responseType = 'json';
xhr.onreadystatechange = function(){
if(xhr.readyState === XMLHttpRequest.DONE){
console.log(xhr.response);
}
else{
console.log(xhr.status);
}
}
xhr.open('POST',url);
xhr.send(attendee);