Closed colonelpopcorn closed 7 years ago
Your server should work fine. You have to dispatch the actions as written here: https://github.com/christianmalek/vuex-rest-api#calling-the-actions
// wrong
this.$store.dispatch("addTodo", body)
// correct
this.$store.dispatch("addTodo", {data: body})
I've changed the dispatch as shown: this.$store.dispatch("addTodo", {data: body});
, and I still get an empty json object on the server. Is there anything special that needs to happen for the data to be sent to the server properly? Does Axios normally send the data object as a request body? I'm also logging my object before sending it and confirming that the expected data is there before sending it.
Edit: I've confirmed with postman that I can receive a body on my express server.
Edit: I figured it out, my object literal was not a properly formatted JSON string. You can close this issue now.
Does Axios normally send the data object as a request body?
Yes. This should work.
I'm also logging my object before sending it and confirming that the expected data is there before sending it.
How do you log it? If you use an axios interceptor you have to return the config (see https://github.com/mzabriskie/axios#interceptors).
I logged it on the server with a console log. I figured out with Postman that it was improperly formatted JSON that was the problem. I fixed it on the client and I can now send items to the server. Thank you for the help!
Glad you find the solution by yourself. :)
I've got a super simple express.js server up and running with body-parser. I can successfully send a request and receive it on the server, but when I log the body of the request it's empty. I tried to make a request with postman and was able to get the object on the server, but I had to set content-type to x-www-form-urlencoded. I went back to my code and created a new Axios instance with content-type set to form-urlencoded, but I still could not see the body of the request on the server.
Here's my store:
Here's the component where I dispatch it.
And my simple express js server.
Here's what I expect when I log the body to the console:
{ body: { tempObject: {description: "Something from the text input", checked: false} } }
Here's what I get:
{}
I think I'm just misunderstanding how the POST requests are done, but I can't figure it out. Is body parser my problem? Should I be looking for a different property in the request object?