Vue-resource: Why change my POST method to OPTIONS?

Created on 2 Apr 2018  路  7Comments  路  Source: pagekit/vue-resource

Reproduction Link


Steps to reproduce

What is Expected?

What is actually happening?

Most helpful comment

Everyone who have this issue can read this post https://dev.to/effingkay/cors-preflighted-requests--options-method-3024
and this
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

In short: if request have Content-type: 'application/json', then CORS policy will be accepted and OPTION request will be also generated

All 7 comments

Probably your method is not changing to OPTIONS, but Chrome makes an OPTIONS request before making the POST request

But is weird, because when I use vanilla js, works fine and don't make that change.

Do you have an example on Codepen or something?

Same error and CORS problem (like some many people here):
image

Code (headers is a proposal from another user in #692 ):

this.$http
          .post(
            $Flinger.paths.apiBase + $Flinger.paths.clientEarly,
            {
              name: name,
              email: email,
              phone: phone
            },
            {
              headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods":
                  "POST, GET, PUT, OPTIONS, DELETE",
                "Access-Control-Allow-Headers":
                  "Access-Control-Allow-Methods, Access-Control-Allow-Origin, Origin, Accept, Content-Type",
                "Content-Type": "application/json",
                Accept: "application/json"
              },
              emulateJSON: true
            }
          )
          .then(response => {
            /* this.message = response.data.message; */
            console.log(response);
          });

These headers...

Access-Control-Allow-Methods
Access-Control-Allow-Headers

..are response headers, and they must be sent by the server, not the client. Your example code above is sending them as part of the request, but they have no effect in a request.

I'd recommend reading this article on CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Everyone who have this issue can read this post https://dev.to/effingkay/cors-preflighted-requests--options-method-3024
and this
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

In short: if request have Content-type: 'application/json', then CORS policy will be accepted and OPTION request will be also generated

umm so interesing, thanks @cre-o

Was this page helpful?
0 / 5 - 0 ratings

Related issues

santigraviano picture santigraviano  路  5Comments

laizhenhai88 picture laizhenhai88  路  4Comments

mikeyao picture mikeyao  路  6Comments

yozman picture yozman  路  6Comments

Dreampie picture Dreampie  路  3Comments