JavaScript fails on sending GET requests to my server - javascript

I have this code:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
httpGetAsync("http://myserver.com/test.php/?u=test&p=testtest");
when I try to run it on Firefox's console, I get:
NetworkError: A network error occurred.
Why does this happen? How can I fix this?

Sending GET request if you are developing a browser extension
Go to your manifest.json file and add permission for www.example.com:
{
"name": "My extension",
...
"permissions": [
"http://www.example.com/*"
],
...
}
Then in your background page (or somewhere else) you can do:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.example.com/test.php?foo=bar", false);
xhr.send();
var result = xhr.responseText;
Reference
How do I send an HTTP GET request in chrome extension?

Related

Execute a http request response in javascript

I have an http request which delivers 'JSON.stringify(data)'.
var xhr = new XMLHttpRequest();
xhr.open("GET", "/api/hello", true);
xhr.send();
xhr.onreadystatechange = function () {
console.log(xhr.responseText);
};
How can I run the code and print the contents of data?
your code should be working, the endpoint may be the problem, check the url your trying to get into the endpoint from, then don't forget to check the readyState and the status of your request before doing nothing.
xhr.onreadystatechange = function () {
if (xhr.readState === 4 && xhr.status === 200)
{
console.log(xhr.responseText);
}
};

Unable to post json data using javascript

function myAjax(type, url, data){
url = type == "GET"? (url+"?"+data): url;
var xhttp = new XMLHttpRequest();
if (!window.XMLHttpRequest) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open(type, url, true);
xhttp.setRequestHeader('Content-Type', 'application/json');
console.log(JSON.stringify(data));
type=="GET"?xhttp.send():xhttp.send(JSON.stringify(data));
}
myAjax("POST","http://localhost:8008/testv/",
{csrfmiddlewaretoken:"{{csrf_token}}",sss: 'sssssssss',});
I'm using django 2.0 with python 3.6. I know csrf_token is required to post data and I am passing it. But still in javascript console I get:
403 Forbidden error.
In servers console I get:
CSRF token missing or incorrect.
What am I doing wrong here?

Can't download file with HTTP request: response for preflight is invalid

I'm trying to use an HTTP request in JavaScript to retrieve a file from the Slack API. I managed to get the url_private_download of the file, but if I try to make an HTTP GET request using that URL, I get an error in the console saying that: Failed to load url_private_download: Response for preflight is invalid (redirect).
Here's my code:
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
console.log(xmlHttp.responseText);
}
};
xmlHttp.open("GET", url_private_download, true);
xmlHttp.setRequestHeader("Authorization", "Bearer " + my_slack_token);
xmlHttp.setRequestHeader('Access-Control-Allow-Headers','*')
xmlHttp.send(null);

google sites JS HTTP request

I have a remote server that receives GET requests with parameter and returns a string.
I want to add a JS code in a google site page that will HTTP GET to that server and print the response string in a table, using the HTML box with:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true);
xmlHttp.send(null);
}
Google sites does not approve it and rejects the function all together.
What other options do I have?

onreadystatechange only firing once

I have the following code for my request:
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) // state of 4 is 'done'. The request has completed
{
callback(req.responseText); // The .responseText property of the request object
} else { // contains the Text returned from the request.
console.log(req.readyState);
}
};
req.open("GET", url, true);
req.send();
However, the readyState is changing to 1 and firing correctly (I'm seeing it echoed in the console) but it simply won't progress to 2. After awhile it times out and I get this in the console:
Failed to load resource: net::ERR_CONNECTION_TIMED_OUT
Uncaught SyntaxError: Unexpected end of input
Anyone have any idea why this might be?
Put this
req.open("GET", url, true);
req.send();
above this line
req.onreadystatechange = function() {
Sorry all, this ended up being a VPN issue, not a scripting one.
function getLatestfileinAllPath(urls)?
{
for(i = 0;i<urls.length;i++){
run(i)
}
function run(){
var request = new XMLHttpRequest();
request.open('POST', url[i]);
request.send(JSON.stringify({"data":"some data"}));
request.onreadystatechange = function()
{
if (request.readyState === XMLHttpRequest.DONE && request.status == 200)
{
console.log(JSON.parse(request.response));
}
}
};
}
}

Categories