GET Method failed to return resource - javascript

I am hosting a webpage in WAMP and keep on receiving this error in the Chrome Developer Console
GET http://localhost/surveys/Internet.json survey.js:368
my survey.js line 368 is in this function`
function loadJSON(key,json){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var obj = xhr.responseText;
localStorage.setItem(key,obj);
}
}
};
xhr.open("GET", json, true);
xhr.send(); // <<-------------------------- line 368
}
I have no clue what is going on. I recently tried to reinstall wamp hoping it was just a conflict with another application but it did not solve anything.
Any help would be great.

It was a problem in the CACHE MANIFEST file
It must be noted in the manifest that these files need internet connection to become available which in my case I did not.
CACHE MANIFEST
#Files to store on application cache
NETWORK
#Files that would require internet connection to access

Related

Javascript XMLHTTPRequest send with protocol undefined in header

I am trying to debug a functionality that runs from a plain old Javascript Web Page and requests to a server.
This perfectly works on my computer but fails on another (the real target)
When it fails, i get an empty string response from the server.
Here is the code that build the request
// Send request to web server
var url = "/start?f="+filesDesc[iFile].name+"&ft="+ft+"&t="+time0ms;
var req = new XMLHttpRequest();
if (req) {
req.open("POST", url, true);
// Hack to pass bytes through unprocessed.
req.overrideMimeType('text/plain; charset=x-user-defined');
req.timeout = 2000;
req.onreadystatechange = function(e) {
// In local files, status is 0 upon success in Mozilla Firefox
if(req.readyState === XMLHttpRequest.DONE) {
var status = req.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
console.debug(req.responseText);
} else {
console.debug("startPlaying : error while sending rqst" );
}
}
};
req.send();
}
I noticed that on my computer (working) the output header of the request looks like this :
POST /start?f=2021-02-09_14;05;40&ft=1612880820756.4346&t=1614243685530 HTTP/1.1
On the target computer (FAIL) it looks like :
POST /start?f=2021-02-09_14;05;40&ft=1612879543815&t=1614183852864 undefined
Notice the "undefined" protocol information
I wonder what can produce such a difference knowing that :
The computer are the same 'Asus ZenBook'
Navigator are the same : Mozilla Firefox 85.0.2 (32 bits)
Network drivers are the same
Client and Server code are the same.
This is very strange behaviour.
Many thanks for any precious piece of information !
We find out that this behaviour was a side effect of a DOM exception caused by registering activeX filters. Our application also tried to load video with calls to :
navigator.mediaDevices.getUserMedia({video: { deviceId: device.deviceId }})
This was ending in an :
Uncaught DOMException: A network error occured.
Believe me or not, removing activeX filters removes the network error !
We felt into a problem similar to :
NotReadableError: Failed to allocate videosource

xmlHttpRequest GET for responseType arraybuffer gets empty response in electron

I am using electron version 1.7.9.
I need to download a zip file in my app.
Following code is used for the same.
var urlFetch = <path to the zip file I want to download>
var xhr = new XMLHttpRequest();
xhr.open('GET', urlFetch, true);
xhr.responseType = "arraybuffer";
xhr.onloadend = function () {
var status = xhr.status;
if (status == 200) {
if (undefined !== xhr.response) {
successCallback(xhr.response);
} else {
failureCallback();
}
} else {
console.error("Failed to fetch " + urlFetch + ", status - " + status);
failureCallback();
}
};
xhr.send();
On Windows 10 as well as on MacOs HighSierra, this code always gets undefined xhr.response in spite of status being 200.
xhr.responseText is populated with string, but what I need is the binary data of the zip file that I can use to store into a file for further use.
I have also tried adding following, but with no success.
xhr.setRequestHeader("Content-Type", "application/zip");
Is there anything missing in the way I am coding or is this a bug in electron/chrome etc.?
The same code works perfectly outside of electron in a stand alone HTML.
The problem was with the node js module xmlHttpRequest which was included.
Things started working when I removed this module.
I was not able to see the XHR requests in the "network" tab of dev tools in my electron app.
After removing the module, I can now see the requests and things work as desired.

Cross-domain XMLHttpRequest request fails in headless chrome

As the title says, I'm having problems making an headless chrome bot execute a XMLHttpRequest to another domain. The bot executes this code:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:30000/login', true);
xhr.withCredentials = true;
xhr.send(null);
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
new Image().src='https://requestb.in/127kh4s1?c=OK';
} else {
new Image().src='https://requestb.in/127kh4s1?c=error-'+(xhr.status);
}
}
};
In my request bin the request are always ?c=error-0, indicating a fail with status code 0. When I visit the page manually, I get c=OK, which leads me to believe it's a problem with my bot.
From there I don't really know what to look for... The bot uses chrome-remote-interface to interact with a chromium browser. The browser is started with these flags: "--headless", "--no-sandbox", "--disable-web-security".
Any suggestion what I should try next?

Ajax Call empty response on Google chrome Mobile

i searched for an answer that fix my problem a lot, but none of the topic fit my scenario..
I have to make an AJAX call inside my application, it work fine on ALL desktop browser, and on SOME mobile browser (for example on my ASUS zenPhone native browser it work correctly, even on my iPhone from work (FF and Safari)) but no way in google Chrome (mobile), in this one the call complete but the response it's empty (only empty, no error provided)... i ask some friend to test it too and similar result occours (empty response) .... i have an https server and an https endpoint
there is my code:
<script>
var x = Math.floor((Math.random() * 10000000) + 2000);
var data = JSON.stringify({
"Token": x,
"Subject": "testAPI"
});
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
//console.log(this.responseText);
var dataJ = JSON.parse(this.responseText);
var dataA = dataJ.Questions;
alert(dataA[0].img);//this is already empty on my mobile :(
dataA.forEach(function(entry) {
//console.log(entry);
});
}
});
xhr.open("POST", url);
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
</script>
Server side CORS are enabled, and as i said it works flawless on all desktop i tested on ... i don't know if i can provide the url to you guys(i have to ask # the API provider) but if you give me some hints it would be nice ...
thanks a lot for your time!
[EDIT]
after some trouble i get an error(Testing remotly from my phone to my PC with dev tools)
Failed to load resource: net::ERR_INSECURE_RESPONSE
on the other device i didn't get this error...
This is a long shot but, try to set the content type header to :
xhr.setRequestHeader("content-type", "text/plain");
This should suppress the CORS preflighting done by chrome which causing the empty response.

XMLHttpRequest responseText is always empty in Firefox WebExtension

I have a problem while writing a Firefox WebExtension add-on. The following code doesn't return any data (responseText is empty), even when the request status equals 4. In Chrome, everything is working perfectly.
I checked even on Fiddler and I can see the request is processed (and we got a response) but it looks Firefox cannot read it?
var xhr = new XMLHttpRequest();
xhr.open('GET', "http://pi.com//", true);
xhr.withCredentials = true;
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(null);
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState == 4) {
console.log(xhr.responseText);
}
};
I really have no clue why Firefox is not getting the responseText filled. I already checked it with different websites, but everywhere it's the same.
In your manifest.json try adding the key "permissions": ["http://pi.com/"]
I was dealing with exactly the same problem, when porting my extension from Chrome to Firefox, it was driving me crazy!
In my case it was as simple as the mailformed URL.
Instead of http://example.com/ I had to state http://www.example.com/

Categories

Resources