The axios.post
(code below) must send data to url api/add-todo
, but I get these errors:
axios.post('http://localhost/vueoctober/todo/api/add-todo', todo).then(function (response) {
console.log(response);
}).catch(function(error) {
console.log(error);
});
The route api/add-todo
is handled with October method Route::get()
(https://octobercms.com/docs/services/router). Why is it not found?
If I change axios.post
to axios.get
it will be working! But I need post data, not get.
What I tried:
1) I tried to add these headers to .htaccess:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTION"
It’s working only for axios.get
. The axios.post
is still blocking.
2) I added Header set Access-Control-Allow-Origin "*"
to httpd.conf.
Vue app is serving at port 8080, therefore axios.post url can’t be relative.
5
Answers
Hours of googling and I got answer...
1) Install plugin
Cross-Origin Resource Sharing (CORS)
.2) In htaccess of Vue app add:
NOTICE! Write SET not ADD!
That's it.
Look at the error message carefully, it says the response to the preflight request didn’t have an HTTP ok status.
Clearly, your server-side code doesn’t have a route handler for the OPTIONS request, so you need to add one.
As an aside, after the browser gets a successful OPTIONS response, it will make the POST request but you said:
You’ll need to use
Route::post()
to handle that.So for clarification on this. There are always numerous ways to answer a problem. Here is what I did for mine. Check this for Preflight Request. The preflight request is created by the browser and is not for security. This function I created first will throw an okay message upon a request, then if the data contains any data it will then do what it is called (this is where you check for security). I don’t have to mess with .htaccess files. Though I did install the CORS plugin because it is a nice plugin. Also the video from watch-learn does the author is making a cross-origin request in which he goes over how to correct the problem. I think he just filmed the video before preflight requests started to be a browser norm. Found routing information here.
I also stumbled and struggled with this on FF, even though I have this in the .htaccess:
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
.After more searching I found a Gist by @marcomessa that fixed my issues.
https://gist.github.com/marcomessa/54d077a0208439f5340126ff629a3718
I can not resolve it via axios, I wasted a lot of hours, but I resolved it very easy by this way.
Let’s think we are posting:
I did the next function to send the last JSON (any JSON structure works):
You can invoke this function, it needs changes the "uri" variable value and it’s required to use JSON.stringify to send the data.
In PHP the API is very easy too, for dummies:
I hope it can help you, any question is allowed and if I have the answer I’ll glad to help.