Chrome extension XHR status 200 but not log on Apache logs - javascript

I'm developing a Chrome extension (my first) and I don't understand one thing.
I call a PHP page on my server with XHR (fetch have the same result). My console says that's this request is OK with status 200, but I have no log of this request on my Apache log.
When I call the page by myself directly on the address bar, I see the page on the Apache log, and the PHP script doing its job.
My javascript code (chrome extension):
var url "http://server/page.php?t=[token]";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { console.log(xhr.status); }
}
xhr.onerror = function () { console.log("Error 1"); };
xhr.send();
The data of the PHP page is:
{ "error": "0" } // Or 1 / 2
Have you already saw the same issue or do you understand what I'm doing wrong?
Thanks.

Related

No Access-Control-Allow-Origin header JS Get Request

I Have write a code using JS, to send GET Request to a URL. This error appears in the console when I'am clicking the button to make the GET request: ERROR
Below you can see the code, I saw some solutions but it didn't work for me. I'am working in a index.html file should I insert something in my code to fix this error?
function call()
{
var url = "http://urlexample";
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};
xhr.send();
}
<button onclick="call();">Make a request</button>

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?

What should a proper GET request and response look like in Node.js

I am working on a small project using Node.js.
The objective is to send an HTTP Request to an array of websites and display what they return to me.
First someone helped me to figure out that I needed a specific Node.js module (XMLHttpRequest). So I "required" it after installing it with NPM. Then I instantiate it.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
//I don't think I need this
xmlHttp.send(null);
//Log some stuff to the console, namely the "response"
console.log(xmlHttp.responseText);
console.log(xmlHttp.statusText);
console.log("Finished!");
Now I believe what this will do is send a GET message to "theUrl", and then save the response in the xmlHttp Object's responseText member.
So now I should have a response. I should be able to print that as text (console.log(xmlHttp.responseText);). What should be in this response?
I expect to get something like "200 OK" but that is nowhere in the response I get. Am I going about this in the right way?
I plan on using the Async Node.js module to send a request like this to an array of URLs, trim up their response (name of the website name, the response status code, and each of the response headers).
You can use below;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
// responseText => response body as string
// status => 200 is OK, 404 page not found
}
};
xhr.open("GET", "yor_url");
xhr.send();
responseText: response body as string
status: 200 is OK, 404 page not found

GET Method failed to return resource

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

Categories

Resources