2017-05-30 78 views
0
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)); 

這是我的代碼。 Iam獲得401未經授權的狀態碼並且未連接。我已經通過發送方式在身上傳遞憑證。任何人的幫助將不勝感激?xmlhttprequest引發401狀態代碼

回答

0

嘗試加入這一行,你調用服務器xhttp.send()

xhttp.withCredentials = true;