Search code examples
ruby-on-railsxmlhttprequest

XMLHttpRequest.withCredentials in Ruby on Rails


I want to make HTTP requests from the browser to the server configured with XHR's withCredentials: true.

I found this documentation of java script.

Is there any similar thing in ruby on rails?

Is this config.http_authenticatable_on_xhr = true anyhow related to my requirement?

Thanks.


Solution

  • I fixed this by adding the below lines in a JS file and placing the file in the path app/assets/javascripts/ and including it in the app/assets/javascripts/application.js as //= require dummy_request

    dummy_request.js contains:

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://example.com/', true);
    xhr.withCredentials = true;
    xhr.send(null);
    

    Using the above technique you can send the request for the first time page being opened. If you want to make request for every particular interval, you can use something like below:

    setInterval(function(){     var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://example.com/', true);
        xhr.withCredentials = true;
        xhr.send(null); }, 3000);
    

    Using the above code, you can send the request at an interval of every 3sec.

    Thanks.