XMLhttprequest返回空的responseText和readyState == 4(WickedPF) [英] XMLhttprequest returning empty responseText and readyState == 4 (WickedPF)

查看:441
本文介绍了XMLhttprequest返回空的responseText和readyState == 4(WickedPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们在rails项目中有一个HTTP请求调用,它运行良好,一切都很好.它会调用controller方法并从该控制器返回值(在这种情况下将为"true"或"false")

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();

现在,我们要将其导出为pdf,并将其导出为wicked-pdf.苦苦挣扎之后,由于Wicked PDF将JavaScript转换为某些本地文件,并且由于CORS,我们在调用controller方法时遇到了问题,现在我们成功地调用了具有cookie的controller方法.因此,调用了该方法,但是,正如我在一开始所说的那样,在正常情况下,responseText为空,这不是因为它正确地构建了HTML.

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

,我不知道为什么要对此进行很多搜索,但我有些困惑.为什么这在正常项目中起作用,但是当尝试从本地文件执行所有这些操作时,尽管没有给出任何错误,但它没有任何作用,但是来自rails的日志是:

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)

我试图增加javascript-delay的时间,因为也许它需要更多时间在控制器中进行计算,但是什么也没有. responseText仍然为空.

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.

此外,我们正在检查HTTP status == 200,但是随后发现,对于本地文件,当其成功执行时,它始终返回状态4(正在返回),因此显然没有错误.那么,该请求如何访问控制器方法,执行所有操作并什么都不返回呢?

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?

推荐答案

您是否尝试过使用AJAX调用来完成这项工作?

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>
});

这篇关于XMLhttprequest返回空的responseText和readyState == 4(WickedPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆