xmlhttprequest抛出401状态代码

时间:2017-05-30 10:37:09

标签: xmlhttprequest

   var json ={};
                json.client_id= "webshop_client";
                json.client_secret= "639a9118-b4ff-4617-86b2-50dcd20af961";
                json.grant_type="password";
                json.username="someusename";
                json.password ="somepassword";


                 var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
                 xhttp = new XMLHttpRequest();
                 var url1="somrurl";
                  xhttp.onreadystatechange = function () {
                      console.log(xhttp.readyState+"||"+xhttp.status);
                      if(xhttp.readyState == 4 && xhttp.status == 200){
                          console.log(xhttp.responseText);
                         service_response = JSON.parse(xhttp.responseText);
                         accessToken = service_response.access_token;
                         return callback(accessToken);
                      }
                  }
                  xhttp.open("POST",url1,true);
                  xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                  xhttp.send(JSON.stringify(json));

这是我的代码。我得到401未经授权的状态代码而没有连接。我已经在send方法中传递了body中的凭据。任何帮助将不胜感激?

1 个答案:

答案 0 :(得分:2)

在使用xhttp.send()

调用服务器之前尝试添加此行
xhttp.withCredentials = true;