React JavaScript Axios在我的代码中有一些错误,如何优化这些数据以供使用

pxq42qpu  于 2023-04-20  发布在  iOS
关注(0)|答案(2)|浏览(78)

这是我的代码,这是导致问题。我有Axios,我不知道如何有效地使用它。

import axios from "axios";

const options = {
  headers: {
    'content-type': 'application/json',
    ContentType: 'text/json; charset=utf-8',
    'X-RapidAPI-Key': 'REACT_APP_KEY',
    'X-RapidAPI-Host': 'rdrunnerxx-trackservice.p.rapidapi.com'
  },
  data: '{"Locations":[{"LatLong":{"Latitude":39.767968, "Longitude": 64.421725}},{"LatLong":{"Latitude":40.022882 , "Longitude": 64.516878}}],"RouteOptions":{"DistanceUnit":0,"RouteOptimize":0,"Culture":"en-US","MapSize":null,"RouteColor":null}}'
};

export const ApiService = {
    async fetching() {
        const response = axios.post('https://rdrunnerxx-trackservice.p.rapidapi.com/route', data,  { options })
        return response
    },
}

const response = axios.post('url', data,  { options })
const data = ?
huus2vyu

huus2vyu1#

我认为你需要编辑ContentType为“Content-Type”:“application/json”

const {data} = await axios.post('/user', document.querySelector('#my-form'), {
  headers: {
    'Content-Type': 'application/json'
  }
})

文档:https://axios-http.com/docs/post_example

vyswwuz2

vyswwuz22#

这个应该能用

async function fetching() {
  try {
    const response = await axios.post(
      'https://rdrunnerxx-trackservice.p.rapidapi.com/route',
      options.data,
      { headers: options.headers }
    );

    const data = response.data;
    console.log('data', data);
    return data;
  } catch(error) {
    console.log(error)
  }
}

export const ApiService = {
   fetching,
}

使用“提取”功能

// component.jsx

useEffect(() => {
  fetching();
}, [])

相关问题