2017-08-14 71 views
0

所以我有一個網頁API設置爲生成一個令牌爲您返回時發送一個用戶名和密碼是相同的,這是通過郵遞員如果你的作品給身體像這樣React Js Axios發佈請求不從網頁api返回機構

enter image description here

這個作品它給你一個令牌和正確的信息,但是當我從瀏覽器,Firefox做,或即它給了我一個錯誤我的代碼是這樣的,

var d = { 
     username: Uname, 
     surname: Pword, 
     grant_type: "password", 
     client_id: "099153c2625149bc8ecb3e85e03f0022", 
    }; 
    console.log(d); 

    var self = this; 
    return (
     axios.post('http://localhost/hydraauth/oauth/token', { 
      headers: { 
       'Content-Type': 'application/json', 
      }, 
      data: d 
     }).then(function (response) { 
      console.log(response.data) 
      self.setState({ 
       isLoggedIn: true, 
       Uname: Uname, 
      }, function() { 
       sessionStorage.setItem("Login", true); 
       sessionStorage.setItem("Uname", Uname) 
       window.location.href = "/"; 
      }); 
     }).catch(function (error) { 
      if (error.response){ 
      console.log(error.response) 
      console.log(error.response.data); 
      console.log(error.response.status); 
      console.log(error.response.headers); 
      } else { 
      window.alert("Incorrect Username Or Password Or Unauthorized To Access This.") 
      } 
     }) 
    ); 

它給了我這個錯誤在chro我

enter image description here

這裏是錯誤擴大了網絡選項卡的

enter image description here

圖像中鉻的響應選項卡爲空

enter image description here

+1

我想問題是由於[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) –

+0

@BardiaRastin它怎麼可能是Cors當它從郵遞員 –

+0

工作,因爲CORS只存在於瀏覽器... –

回答

1

愛可信與問題通過主體發送數據,因此您需要使用npm plugin query string

,並用它像這樣

axios.post("http://localhost/hydraauth/oauth/token", querystring.stringify({ "username": Uname, "password": Pword, "grant_type": "password", "client_id": "eyJjbGllbnRfaWQiOiJTYXZ2eS1Dcm0tUmVhY3QtSnMtU3lzdGVtIn0" }))