XMLhttprequest returning empty responseText and readyState == 4 (WickedPF)

筅森魡賤 提交于 2020-02-04 01:18:06

问题


So we have this HTTP request call in our rails project which is working good, everything is fine. it calls the controller method and returns the value from that controller (in this case is going to be "true" or "false")

var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
      }
    }
    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.open('GET', url);
    httpRequest.withCredentials = true;
    httpRequest.responseType = 'text';
    // httpRequest.setRequestHeader('Content-type', 'application/json');
    httpRequest.onreadystatechange = function(){
      $container.find('h4').html(JSON.stringify("Error"));
      if (httpRequest.readyState === 4) {
        $container.find('h4').html(JSON.stringify(httpRequest.response));
        // $container.find('h4').html(JSON.stringify("SUCCESS"));
      }
    };
    httpRequest.send();

Now, we want to export this to pdf, with the gem wicked-pdf. After struggling with this, since Wicked PDF converts the JavaScript to some local files and we had problems calling the controller method, because of the CORS, now we successfully call the controller method having a cookie. So, the method is called, but, responseText is empty when in normal conditions as I said at the beginning, it is not since it's building the HTML correctly.

So, the request is okay, is getting to the controller method, and is doing everything, but apparently this is not working:

render :json => @status, :layout => false

and I don't know why I've searched a lot about this and I'm kind of stuck. Why this is working in the normal project, but when trying to execute all this from local files, it doesn't, although is not giving any errors, the logs from rails are this:

INFO -- : Started GET "/monitor/devicestatus_alarms/30" for ::1 at 2020-01-24 09:22:18 +0000
INFO -- : Processing by MonitorController#devicestatus_alarms as JSON
INFO -- :   Parameters: {"id"=>"30"}

INFO -- : Completed 200 OK in 45ms (Views: 0.2ms | ActiveRecord: 18.3ms)

I tried to increment the javascript-delay because maybe it required more time to do the calculations in the controller but nothing. responseText is still empty.

Also, we were checking for HTTP status == 200, but then we found out that with local files, when it succeeds, it always returns a status 4, which is returning, so apparently there are no errors. So, how can this request access the controller method, do everything and return with nothing?


回答1:


Have you tried using AJAX calls to get this job done?

$.ajax({
  url: "<your_url>",
  type: 'GET',
  dataType: 'text',
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  }
}).then(function (data) {
  < actions here >
  }
}).always(function () {
  < always action here>
});


来源:https://stackoverflow.com/questions/59895869/xmlhttprequest-returning-empty-responsetext-and-readystate-4-wickedpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!