I'm new to the Phoenix framework, and I've been struggling for days to get my React frontend project to speak to my Phoenix JSON API. I generated the API code using one of the generators. I'm able to successfully make POST requests with an API tester (reqbin.com), but when I try to make a request with the same body from my page, I always get back a "400 Bad Request" response. It seems likely that this is do to some Header nuance I'm missing, but for the life of me I can't figure it out.
Here's my router code:
pipeline :api do
plug :accepts, ["json"]
plug CORSPlug, origin: "*"
end
scope "/api", HelloWeb do
pipe_through :api
get "/", PageController, :index
get "/test", PageController, :test
resources "/users", UserController
resources "/apps", AppController
And my request code from my react app.
var body = {app:{"name":"Phoenix_Test"}}
console.log(body);
var that = this;
var schema = fetch(db_url_var, {
method: 'POST',
body: body,
mode: 'no-cors',
headers: {
'Content-Type': "application/json",
Accept: "application/json"
}
}).then(function(res){
alert("Saved!")
// that.setState({"hidden":true})
})