如何使用 React.Js 使用基本身份验证发出 axios.post 请求?

时间:2021-07-07 09:38:53

标签: javascript reactjs axios

我正在使用 React.Js、Redux 和 Basic Auth 发出 axios.post 请求,但我发现了这个错误。 请问我该如何解决?

我正在使用:Front (React.Js)、Back (SpringBoot)、Data Base (MySQL)

这是我的代码:

const allDev = []
export const createGps = (gpsDevice) => {
  let uri = "http://62.171.157.49:8081/api/devices";
            
  let h = new Headers();
  h.append('Accept', 'application/json');
  let encoded = window.btoa('yahya.mess95@gmail.com:Yahya@2027');
  let auth = `Basic ${  encoded}`;
  h.append('Authorization', auth );
  console.log( auth );
  
  let req = new Request(
    uri, 
    gpsDevice,
    {
      method: 'POST',
      headers: h,
      credentials: 'include'
    }
  );
    return (dispatch) => {
      axios.post(req)
      .then( (response)=>{
        console.log(response)
          if(response.ok){
              dispatch(notifAdd())
              console.log(response.json())
              return response.json();
          }
          throw new Error('BAD HTTP stuff');
          
      })
      .then( (jsonData) =>{
          console.log(jsonData);
          JSON.stringify(jsonData, null, 4);
            return {
              ...gpsDevice,
              allDev: [...allDev, {...gpsDevice}]
            }
      })
      .catch((err) => console.log("error adding dev: ", err))
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我使用 Traccar 的 API。