简体   繁体   中英

how to send data in GET request header using react and axios to Django rest framework?

i am new to react and axios and i want to know how to send data in my headers using get request i tried this but it didn't work

** i observed that the request type changed to options in django terminal window **


export const getUserOrders=(userID)=>{
    return dispatch=>{
        dispatch(getUserOrdersStart())
        axios.defaults.headers={
            'Content-Type':'application/json',
            id:userID
        }
        axios.get('http://127.0.0.1:8000/admindashboard/userorders/')
        .then(res=>{
            const order=res.data
            dispatch(getUserOrdersSuccess(order))
        }).catch(err=>{
            getUserOrdersFail(err)
        })
    }
}


Try adding CORS headers

Use this package and read the docs

https://github.com/adamchainz/django-cors-headers

You can give the approach below a whirl.

export const getUserOrders = async userId => {
      const header = {
        "Content-Type": "application/json;charset=utf-8",    
      };
      let id = userId;
      const URL = `http://127.0.0.1:8000/admindashboard/userorders/`
      return dispatch => {
        try {
          dispatch(getUserOrdersStart())

           const response = await axios(URL,id, {
            method: "GET",
            mode: "cors",
            headers: header,

          });
          const jsonResponse = response.json();
          dispatch(getUserOrdersSuccess(order))
        } catch (error) {
          setError(error);
        }
      };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM