I have a function called create(conn, params)
in my controller that gets called when some data is posted to my server from a form on the client – the form was built using Phoenix templates.
All I do is extract some data from the params, do something with it, and then render another template in response.
Now I would like to write myself a little javascript helper function so that I can click something in my browser and have this data posted from JS – i.e., I just want to post the same data I would otherwise post, except without going through the form.
Everything seems to work, I can see the data arriving at my server, and the params are what I expect. But no response is ever rendered.
code inside my create(conn, params)
function looks like this:
IO.inspect("I get this far okay")
conn
|> put_flash(:info, "Found ID: #{the_id}")
|> render("confirm_sheet.html", title: title, some_data: some_data)
I don’t see any error message on the server or client – it’s like my code just prints out “I get this far okay” and then vanishes into thin air.
But then if I go post the data from my form, I see the response in the confirm_sheet.html
template as expected.
The only difference I can think of is that I am posting JSON instead of some other format. Here is the JS function that I am using to try to call my route:
<script>
window.quick_post = function(gsheet_id) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:4000/sheets", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
var to_send = JSON.stringify({
"_csrf_token": "<%= Phoenix.Controller.get_csrf_token() %>",
"sheet": {"the_id": the_id}
});
xhr.send(to_send);
}
</script>
So what is happening here? How can a route give no error / warning and also no response?