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?
JSON.stringify()
with a string (.html(JSON.stringify("Error"))
)? o.O – Andreas