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?
Related
How do I check, if a POST XHR request was successful, on a website, for a particular URL?
If the POST was successful, I then want to run some JavaScript.
I've seen ways to do this in jQuery, but I want to do this via vanilla JavaScript.
You can use the status code to check if the request was successful:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
// successful
}
}
xhr.open('GET', 'http://example.com', true);
xhr.send();
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status
https://stackoverflow.com/a/3038972/10551293
I am making an api call to
http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter
I have changed the appropriate information in the URL to reflect the correct host address/port / user id.
When that is complete, the page requests that I log on with a Username and Password.
I can manually enter this information, and receive the XML that I need. This is not Ideal. I would rather have a form that passess in this information.
To my understanding, this information is passed within the "headers". I have tried to look up how to do this, and even attempted using postman without much luck. I am not sure how to do this.
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter", true);
xhttp.send();
}
per w3 schools I can use setRequestHeader() which adds a label/value pair to the header to be sent.
I have tried
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "http(s)://{hostaddress:port}/com.broadsoft.xsi-actions/v2.0/user/{userid}/services/callcenter", true);
xhttp.send();setRequestHeader(Username:myUserName,Password:myPassword);
}
with no resolution. once i get this working i will set up the form and pass in the values.
You are using setRequestHeader in the wrong way and calling send before setting headers in any case.
Try with
xhttp.setRequestHeader('Username', myUserName);
xhttp.setRequestHeader('Password', myPassword);
xhttp.send(); // only call send after setting up the headers
xhttp.setRequestHeaders(name, value) and it should be invoked before xhttp.send(). mdn
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);
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?
Here is my java script to call WCF service
function CallWcfAjax() {
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:20949/RestService/tiger/pandey";
// Send the HTTP request
xmlHttp.open("GET", url, true);
xmlhttp.send();
// Create result handler
xmlHttp.onreadystatechange = function X() {
if (xmlHttp.readyState == 4) {
alert(xmlHttp.responseText);
}
}
}
if I enter the same URL in browser http://localhost:20949/RestService/tiger/pandey i get a response.
Any idea what wrong I am doing in Java Script?
Thanks
You variable name is xmlhttp and in the rest of you code, you use xmlHttp. This is a problem.