I've got the following synchronous request...
var xhr = new XMLHttpRequest();
xhr.open("GET",uri,false);
xhr.send(null);
I've read http://www.w3.org/TR/XMLHttpRequest/#the-open-method and it appears that this doesn't allow for extra parameters. Basically I'd like to send other data such as postdata and useragent how would I do this using AJAX?
var strResult;
var xhr = new XMLHttpRequest();
xhr.open("POST",uri,false);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("id=1&user="+txtUser.value+"&password="+txtPassword.value);
strResult=xhr.responseText;
i'm not 100% sure, but i think you can pass .send() extra parameters.
Related
const xhr= new XMLHttpRequest();
xhr.open('POST',url,true);
Let params="name"="Rahul"
xhr.send(params)
Cannot understand why we use url in post request
I'm trying to make a CORS request to Google's static maps api and return an image to be mounted on canvas (to avoid canvas's Cross Origin protection). I'm not sure if I'm following the instructions correctly (https://developers.google.com/api-client-library/javascript/features/cors) but the xhr request never returns anything.
const position = latLngArg;
var xhr = new XMLHttpRequest();
xhr.open('GET',
`http://maps.googleapis.com/maps/api/staticmap?center=${position.lat()},${position.lng()}&zoom=20&size=20x20&maptype=roadmap&sensor=false&key=AIzaSyBiE2efHKeAptVfVRtj9-ZDeHWPKgNjdNk`)
.then(...)
The XMLHttlRequest open method does not return a promise.
It also does not send the request it only initialize it. In order to set the request you need to call the send method afterwards.
You should register a callback to be triggered upon success by setting the onreadystatechange property.
See an example here.
Native XHR do not work with promises.
You can try the following:
const position = latLngArg;
const xhr = new XMLHttpRequest();
const address = `http://maps.googleapis.com/maps/api/staticmap?center=${position.lat()},${position.lng()}&zoom=20&size=20x20&maptype=roadmap&sensor=false&key=AIzaSyBiE2efHKeAptVfVRtj9-ZDeHWPKgNjdNk`;
xhr.open('GET', address);
xhr.onload = () => onImageReceived(xhr.response);
xhr.send();
function onImageReceived(image) {
console.log(image);
}
You can verify that the xhr.response will be your image, after the request was completed (onload event).
Read more about it here.
Is there any way to get content type and content disposition of a url using java-script?
Thanks
Unfortunately, there isn't an API to give you the HTTP response headers for your initial page request.
Accessing the web page's HTTP Headers in JavaScript
Although you could make a new request and read those:
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
I can't seem to be able to get XMLHttpRequest() to work cross domain. I am able to get it to work cross domain via XDomainRequest() but I need to do a synchronous request.
var url = 'http://phpfilethatspitsoutjson.com';
// Can't get this to work
var req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
// This does work
xdr = new XDomainRequest(); // Creates a new XDR object.
xdr.open("GET", url); // Creates a cross-domain connection with our target server using GET method.
xdr.send(); //Send string data to server
xdr.onload = function () {
};
Also I do have header("Access-Control-Allow-Origin: *"); set in my url.
I am new to titanium and and want to call a web service from my titanium app.
The webService returns the json response.
As I am aware of calling the webService using XMLRPC but very confused regarding json.
Until now, I know that we have to create the HTTPClient.
var request = Titanium.Network.createHTTPClient();
request.open("POST", "http://test.com/services/json");
request.onload = function() {
var content = JSON.parse(this.responseText);//in the content i have the response data
};
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //did not understand this line
request.send();
Now the problem is if my url(endpoints) have many WebServices, so where i will give the method name i.e WS name which is to be called.
From the API documentation of Titanium mobile the function open i.e. request.open accepts 3 parameters:
method name (http method name)
url of request
async (boolean property) by default true.
In the above code what is "POST" doing there?? and if my WS name is system.connect then where i will be mentioning that in code?
And what if the WS needs parameter, so how can we send the parameter to the webService form the above code.
I know that request.send() can be used to send parameter but how ??
To invoke a webservice you should:
// create request
var xhr = Titanium.Network.createHTTPClient();
//set timeout
xhr.setTimeout(10000);
//Here you set the webservice address and method
xhr.open('POST', address + method);
//set enconding
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//send request with parameters
xhr.send(JSON.stringify(args));
// function to deal with errors
xhr.onerror = function() {
};
// function to deal with response
xhr.onload = function() {
var obj = JSON.parse(this.responseText);
};
address is your webservice url.
method is the method you desire to invoke.
address+method is a URL, in your example: "http://test.com/services/json" the method invoked would be named json.
args: is a json object where it's variable names should have the exact same name as the webservice parameters. You can create a the parameters object like this:
var args = {};
args.parameter1 = 'blabla';
args.parameter2 = 'blaaaa';