[Vuejs]-How to solve problem with Axios post request

2👍

OK, let’s break this down:

axios.post("... lots of stuff ..."+ this.$store.state.tracker.trackingKeyId , {
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
    'X-Powered-By': 'bacon'
  },
  tracking_data: this.$store.state.tracker.trackingClientData
})

The first argument you’re passing to post is the URL. I’m going to assume that’s the correct URL.

The second argument you’re passing is this object:

{
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
    'X-Powered-By': 'bacon'
  },
  tracking_data: this.$store.state.tracker.trackingClientData
}

This is wrong in multiple ways.

Firstly, you seem to be trying to add response headers as request headers. Neither Access-Control-Allow-Origin nor X-Powered-By should be there. They should be on the server.

But they aren’t actually being set anyway because you’ve put the headers config in the wrong place. The arguments for post should be url, data, config. The headers need to go in the config, not the data.

axios will set the content-type header automatically. In the case of your request it’ll set it to application/json. However, that will trigger a CORS preflight OPTIONS request as it isn’t one of the 3 safe-listed content-types.

I don’t know what content-type the API you’re using expects but if you need to avoid the preflight request then you’ll need to set it to one of application/x-www-form-urlencoded, multipart/form-data or text/plain. e.g.:

axios.post("... lots of stuff ..."+ this.$store.state.tracker.trackingKeyId , {
  tracking_data: this.$store.state.tracker.trackingClientData
}, {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

However, the data is still being encoded as JSON here (so the content-type header and the request body don’t actually match). I believe jQuery defaults to encoding the data as a URL encoded query string. The official axios documentation for doing that is here:

https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format

However, all of this very much depends on exactly what format the server is expecting.

As you only have a single parameter it might be possible to do something like this:

axios.post(
  "... lots of stuff ..."+ this.$store.state.tracker.trackingKeyId,
  'tracking_data=' + encodeURIComponent(this.$store.state.tracker.trackingClientData),
  {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }
)

To debug this further you should carefully inspect the headers being sent each way in the Network tab of your browser’s developer tools. Make sure they are exactly what you’re expecting. There shouldn’t be any mysteries here, you can see exactly what headers are being used.

Further, if you aren’t clear on how CORS works and what can trigger a preflight OPTIONS request then you should do some background reading on that.

Finally, you need to find out exactly what format your server is expecting for the data. We can speculate all day about what the correct code might be but if we know what we’re aiming for then there’s no need to guess.

I would add that the jQuery example you posted has tracking_data spelt incorrectly as traking_data. That won’t be related to the CORS error but it makes me wonder whether it really ‘works’.

1👍

The issue means server restricts cross-origin requests. Those headers you’ve tried to add should be added on the server side. There is no way you can make it work without modifying your server code/settings.

Leave a comment