Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you send JSON data in a rails-ujs Rails.ajax POST call (not using jQuery)?

Tags:

I have a React client app that needs to talk to a Rails API. I want to use the rails-ujs method Rails.ajax. For example:

Rails.ajax({   type: "POST",    url: "/things",   data: mydata,   success: function(response) {...},   error: function(response) {...} }) 

It looks like I can't set data to a JSON object like this:

mydata = {  thing: {   field1: value1,   field2: value2, }} 

I need to convert it to a application/x-www-form-urlencoded content type manually like this:

mydata = 'thing[field1]=value1&thing[field2]=value2' 

This is ok for flat data but gets complicated quickly for nested data.

jQuery does the conversion automatically before making a request.

So I'm wondering if Rails UJS has some automatic way of doing it, but I couldn't find anything in the docs or code.

like image 795
Hrishi Mittal Avatar asked Aug 22 '17 14:08

Hrishi Mittal


People also ask

How rails send data using AJAX?

Use rails-ujs (no jQuery) Making an AJAX POST call with rails-ujs looks identical to making it with jQuery: Rails. ajax({ type: "POST", url: "/things", data: mydata, success: function(repsonse){...}, error: function(repsonse){...} })

Can AJAX send JSON?

By using the jQuery ajax method we can call them or we can say that we can request the different types of text and post such as HTML, XML, and JSON from the remote server as well as it uses the get and post method that is HTTP protocol.

How use jQuery AJAX in Rails?

In Rails, submitting an AJAX request can be done as easily as adding remote: true to a link, button, or form. From there you can have any response be some JavaScript code waiting on the server side, and it will execute in the client's browser. Here's the simplest code example of UJS via AJAX in a link.

How does AJAX work with JSON?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.


2 Answers

When using Rails UJS, data must be formatted as string form parameters. Unfortunately, it's not possible to provide JSON, at least not currently with Rails 6.0.2 (not ideal).

Solution

Use URLSearchParams to convert JSON to URL paramaters:

myData = {  thing: {   field1: value1,   field2: value2, }}  Rails.ajax({   type: "POST",   url: "/things",   data: new URLSearchParams(myData).toString() }) 
like image 163
Jeremy Lynch Avatar answered Sep 22 '22 17:09

Jeremy Lynch


Here is my workaround to put/post with Content-Type: application/json.

It works by setting options.data in the beforeSend callback. This way the internal Rails.ajax.createXHR method does not set the Content-Type header first. https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee#L53

    Rails.ajax({       url: 'http://some-url.com',       type: 'put',       beforeSend(xhr, options) {         xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')         // Workaround: add options.data late to avoid Content-Type header to already being set in stone         // https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee#L53         options.data = JSON.stringify(data)         return true       },     }); 
like image 39
Freddy Avatar answered Sep 23 '22 17:09

Freddy