0

I want to make a post request with axios and the data that I must send must be json. I have seen that the content type of my header must be application / json and this is done by default. but when I see the request my called the content type is Content-Type: application / x-www-form-urlencoded

I have tried different ways to set it to change it but it always shows up in the request headers.

The service does the insert but with empty data.

Any idea? I've lost two days with this. Thanks!

const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Authorization'
      }

const dataJson = JSON.stringify(solicitudObj);

  axios.post(state.basePath + 'Api/Solicitud/SaveSolicitud/',
    dataJson,
    headers
  )
    .then(response => {
      console.log(response);
    })
    .catch(error => {
      console.log(error);
    })
Ruben Ortega
  • 51
  • 1
  • 1
  • 3
  • 1
    Just peeking at the docs, I think your 3rd argument may need to be `{ headers }` (see this example https://github.com/axios/axios/issues/858#issuecomment-302257238) – Nick Aug 14 '19 at 23:43
  • Thanks Nick, when I put the headers between braces like that {headers: state.headers}, the Content-Type in the request body is application/json now. But I have a problem with CORS policy. I'm not sure if the problem with the cors is mine or the server. This is the message: has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. I'll keep looking, thank you again. – Ruben Ortega Aug 15 '19 at 14:00

0 Answers0