I came across something that I can’t wrap my head around while building some jQuery forms on a legacy site that are submitting to our new Phoenix-driven API.
The relevant part of the jQuery submit is this:
$.ajax({
type : 'POST',
url : url, // <-- submits to our Phoenix endpoint
data : {foo: "bar"}, // sample data
contentType: 'application/json; charset=UTF-8', // <-- Causes errors!
dataType : 'json'
})
What I noticed is that when the contentType
is explicitly specified, Phoenix gets an error:
a Jason.DecodeError exception was raised with message "unexpected byte at position 0: 0x70 ('p')
The route is using the json pipeline:
pipeline :api do
plug(:accepts, ["json"])
end
but it only seems to work when the request is sent using the content-type application/x-www-form-urlencoded; charset=UTF-8 header.
I feel like I must be doing something stupid here, but I can’t see it. Can anyone set me straight? Thanks!