tadasajon

tadasajon OP

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?

First 3 of 3 Posts Switch mode

Nicd

Nicd

Looking at your JS client code, what do you expect it to do? Seems to me that you told your client to send a request to the backend and then do nothing with the response. The response will not be automatically inserted or replaced into the DOM, you need to write the code to do that.

Keep your browser’s developer tool’s network console open while sending the request. You should see what the server responded. But if nothing is done with the response, then it will indeed “vanish into thin air”.

Now, if you want to replicate the behaviour of a browser’s form – i.e. once you have submitted something, you are taken to a new page that completely replaces the old one – without using a form, you need to actually create a hidden form on the page and submit it in JS. Then it will act like a normal form was submitted. You can accomplish sort of the same thing with XMLHttpRequest by using the response HTML to replace the entire page HTML and updating the URL, but it’s not entirely the same thing.

tadasajon

tadasajon OP

okay, thanks – this is just what I needed to know to make some progress.

I guess I was just expecting the browser to do whatever it automatically does when it gets a response from the webserver – i.e., render the response. I didn’t realize that I also had to handle the response in my JavaScript.

Nicd

Nicd

The browser does not do anything by default for requests sent from JavaScript, i.e. XMLHttpRequest or Fetch API. That’s entirely manual for you to implement. The easiest way to simulate a normal form post is indeed to make an invisible form (with <input type="hidden"> elements), fill it with values, and submit it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement