I need to implement synchronous Ajax calling mechanism. I already implement ajax calling function in my helper same as below :
MH.helper = {
ajax : function (option) {
if(option !== undefined) {
for(var opt in option) {
this[opt] = option[opt];
}
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr=new XMLHttpRequest();
} else {
// code for IE6, IE5
this.xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
I also implement Ajax prototype as same as below :
MH.helper.ajax.prototype = {
// XMLHttpRequest obj
xhr : null,
// request url
url: '',
// post funciton
post: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data, true);
xhr.open("POST",this.url,true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(data);
},
// get funciton
get: function() {
var xhr = this.xhr;
var that = this;
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200){
if(that.complete && ( typeof that.complete === 'function' )) {
that.complete(xhr.responseText);
}
}
}
var data = MH.helper.serialize(this.data);
xhr.open("GET",this.url+data,true);
xhr.send(data);
},
// callback when request done
complete: null
}
Anyone have any idea how can I implement synchronous call using my Ajax function?
xhr.open("POST",this.url,true)
If you pass false as the 3rd parameter instead of true - the call will be performed synchronously.
But my advice - don't. Use callback functions always.
Pass false as the third argument of xhr.open.
Sources: Specification, MDN article.
Related
I'm following a simple book and It says:
function createRequest()
{
try
{
request = new XMLHttpRequest();
}
catch (tryMS)
{
try
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (otherMS)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
request = null;
}
}
}
return request;
}
function getDetails(itemName)
{
var request = createRequest();
if (request==null)
{ alert("Unable to create request");
return;
}
var url= "getDetails.php?ImageID=" + escape(itemName);
request.open("GET",url,true);
request.onreadystatechange = displayDetails;
request.send(null);
}
function displayDetails()
{
if (request.readyState == 4)
{
if (request.status == 200)
{
detailDiv = document.getElementById("description");
detailDiv.innerHTML = request.responseText;
}
}
}
And all this code above is fine and it's okay to me.. but after few pages it says:
ITS VERY IMPORTANT TO REMOVE VAR KEYWORD BEFORE request VARIABLE so the callback can reference the variable...
but how come in example above it worked? is it coincidence if we call a variable 'request' that it will map with global variable in a createRequest method?
Take a look on image below:
Why is this happening ? in one example var before request variable is used and everything is fine, in another var is avoided so the method in callback might access it.. but how come method in a callback is accessing a request variable in first example...
It's confusing because there are 2 similar examples, with different explanations..
EDIT
P.S it says request has to be a global ? :o
Thanks guys
Cheers
In both examples, implicit global variables are created so they can be shared with the callback.
When the second request variable is created, it creates a local variable inside the getDetails function. So when createRequest() returns the global variable, the local variable becomes a reference to it.
This is rather bad advice and shows a lack of understanding on the writers' part. But it seems to be an old text, since activeX objects are deprecated by now, so maybe globals used to be less frowned upon. The proper way is to either send the responseText or responseXML as a parameter to the callback or send the entire request as the parameter for the callback.
Maybe the writer didn't want to make the request code more complex, but imho, this is not a good way to teach people things.
function createRequest( method, url, callback, payload ) {
var request = new XMLHttpRequest();
if ( !request ) {
alert( "Unable to create request" );
return null;
}
request.open( method, url );
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200 ) {
callback( request.responseText );
}
};
request.send( payload );
};
function getDetails( itemName, callback ) {
createRequest( "GET", "getDetails.php?ImageID=" + escape(itemName), callback, null );
};
function displayDetails( detail ) {
var detailDiv = document.getElementById("description");
detailDiv.innerHTML = detail;
};
getDetails( "someItemName", displayDetails );
you are right, in your first example, function createRequest is not using var, which mean you are creating a global variable request when excute request = new XMLHttpRequest();.
We should avoid using gobal var in most situation.
function createRequest() {
try {
// add var so it's not global variable
var request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function getDetails(itemName)
{
var request = createRequest();
if (request==null)
{ alert("Unable to create request");
return;
}
var url= "getDetails.php?ImageID=" + escape(itemName);
request.open("GET",url,true);
// create anonymous function to call your callback and pass `request` as local variable
request.onreadystatechange = function(){
displayDetails(request);
};
request.send(null);
}
function displayDetails(request)
{
if (request.readyState == 4)
{
if (request.status == 200)
{
detailDiv = document.getElementById("description");
detailDiv.innerHTML = request.responseText;
}
}
}
I want to call rest services using javascript. My code is:
function CreateXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
alert("1");
return new XMLHttpRequest();
}
else if (typeof ActiveXObject != "undefined") {
alert("2");
return new ActiveXObject("Microsoft.XMLHTTP");
}
else {
throw new Error("XMLHttpRequestnot supported");
}
}
function CallWebService() {
var objXMLHttpRequest = null;
objXMLHttpRequest = CreateXMLHttpRequest();
objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true);
objXMLHttpRequest.setRequestHeader("Content-Type", "application/xml;charset=UTF-16");
var packet = '<CompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></CompanyRequest>';
objXMLHttpRequest.send(packet);
alert(packet);
var d =(objXMLHttpRequest.responseText);
alert(d);
}
It is working fine in IE, but in Firefox and chrome the response is empty. I can't understand what is going wrong. I have been searching for this for 3 days.
Let me know if there are any errors.
Thanks in advance....
You're making async call. Try to use callback.
function CreateXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("XMLHttpRequestnot supported");
}
}
function CallWebService() {
var objXMLHttpRequest = CreateXMLHttpRequest();
objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true);
objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml");
var packet = '<CompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></CompanyRequest>';
objXMLHttpRequest.onreadystatechange = function (){
if (objXMLHttpRequest.readyState==4 && objXMLHttpRequest.status==200) {
alert(objXMLHttpRequest.responseText);
}
}
objXMLHttpRequest.send(packet);
}
You are doing an async request.
Try doing a synchronous request like
objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", false);
Or asynchronusely
objXMLHttpRequest.onreadystatechange = function () {
if ( objXMLHttpRequest.readyState == 4 ) {
alert(req.responseText);
}
};
And set the header to:
Try this:
objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml");
objXMLHttpRequest.setRequestHeader( "SOAPAction", "http://localhost:2546/abc.svc/json/GetXml" );
I have an ajax call where I used jQuery.ajax() to make a request to an mvc action. This all worked fine. However due to some forms having a file control I changed it from using jQuery.ajax() to using the XMLHttpRequest to send it using the HTML5 File API.
Since making this change the MVC action method no longer see's it as an ajax request. Using Fiddler2 I have noticed that it no longer adds the "X-Requested-With: XMLHttpRequest" to the request and I assume this is the problem.
The form I am trying to send does not have a file input in it, only normal textboxes etc, but I was trying to keep the method generic to deal with both. The following is the code I am using to send the ajax request:
// get the edit tender form
var $Form = $Button.closest('form');
var Url = $Form.attr('action');
var AjaxRequestObject = new XMLHttpRequest();
var FormDataToSend = new FormData();
$Form.find(':input').each(function () {
if ($(this).is('input[type="file"]')) {
var files = $(this)[0].files;
if (files.length > 0) {
FormDataToSend.append(this.name, files[0]);
}
} else {
FormDataToSend.append(this.name, $(this).val());
}
});
AjaxRequestObject.open('POST', Url, true);
AjaxRequestObject.onreadystatechange = function () {
if (AjaxRequestObject.readyState == 4) {
// handle response.
if (AjaxRequestObject.status == 200) {
if (!AjaxErrorExists(AjaxRequestObject.responseText, )) {
alert("success");
console.log(AjaxRequestObject.responseText);
}
else {
alert('failure');
}
}
else {
alert('failure');
}
}
};
AjaxRequestObject.send(FormDataToSend);
This code was provided following a problem I had which Darin Dimitrov provided the solution to, so I could send the file inputs by ajax.
Any ideas why this request would not send the header for an ajax call?
X-Requested-With is automatically added by jQuery. You can just as easily add it yourself with AjaxRequestObject.setRequestHeader(). Docs
I was having troubles with detecting if my request was ajax. So, maybe this sample will save someone a minute or two:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', URL, true); // `true` for async call, `false` for sync.
// The header must be after `.open()`, but before `.send()`
xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlhttp.onreadystatechange = function() {
// 4th state is the last:
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { ... }
};
xmlhttp.send();
Tested with Flask.
You can override natively all XMLHttpRequest.open method calls and add in it X-Requested-With header like:
(function () {
// #author https://github.com/stopsopa jfdsa78y453cq5hjfd7s877834h4h3
if (window.XMLHttpRequest.prototype.onOpen) {
return console.log('XMLHttpRequest.onOpen is already defined');
}
function over(method, on, off) {
var old = window.XMLHttpRequest.prototype[method];
if (!old.old) {
var stack = [];
window.XMLHttpRequest.prototype[on] = function (fn) {
if (typeof fn === 'function') {
stack.push(fn);
}
}
window.XMLHttpRequest.prototype[off] = function (fn) {
for (var i = 0, l = stack.length ; i < l ; i += 1 ) {
if (stack[i] === fn) {
stack.splice(i, 1);
break;
}
}
}
window.XMLHttpRequest.prototype[method] = function () {
var args = Array.prototype.slice.call(arguments);
var ret = old.apply(this, args);
for (var i = 0, l = stack.length ; i < l ; i += 1 ) {
stack[i].apply(this, args);
}
return ret;
}
window.XMLHttpRequest.prototype[method].old = old;
}
}
over('open', 'onOpen', 'offOpen')
XMLHttpRequest.prototype.onOpen(function () {
this.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
});
}());
I try to make Cross Domain POST requests and get back JSON encoded responses,
everything works fine except in IE 7, 8, 9.
I have try those solutions but i get this error:
Object doesn't support property or method 'setRequestHeader'
Function createXMLHTTPObject() - attempt 1
function createXMLHTTPObject() {
var xmlhttp = false;
var is_IE = window.XDomainRequest ? true : false;
if (is_IE) {
xmlhttp = new window.XDomainRequest();
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
Function createXMLHTTPObject() - attempt 2
var XMLHttpFactories = [
function() { return new XMLHttpRequest() },
function() { return new ActiveXObject("Msxml2.XMLHTTP") },
function() { return new ActiveXObject("Msxml3.XMLHTTP") },
function() { return new ActiveXObject("Microsoft.XMLHTTP") }
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0; i<XMLHttpFactories.length; i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch(e) {
continue;
}
break;
}
return xmlhttp;
}
Function send()
Here it returns the error, at: req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
function send(postData, callback) {
var url = 'http://domain.com/ajax-processing.php'; //url overlap
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method, url, true);
req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
if (postData) {
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
req.onreadystatechange = function() {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
console.log('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
Here i call the send function
var insert = 'id=1&type=insert';
CLib.send(insert, function(data) {
console.log(data);
});
Is it possible to make Cross Domain requests in IE?
How i can leave behind this part, without using any other library like jQuery?
Thanks a lot #Esailija who inform me that i can't make set request headers with the XDomainRequest.
So i tried other methods and solutions also and i finally came back with a simpler method:
changed the POST requests to GET and everything working fine after some small changes.
Evidently jQuery has made me dumb.
I've got a local url that serves up raw JSON, and I can't figure out how to consume that json from within my method without using jQuery.
Here's how I know to do it WITH jQuery
var myJson;
$.getJSON('/local/path/to/json', function (data) {
myJson = data;
});
// Now I can use myJson in a method.
To retrieve the JSON string from a server use XMLHttpRequest object as described in this reference:
http://developer.mozilla.org/en/XMLHttpRequest
You'll find that it's quite involved with all the unseen things you need to account and check for. Thus libraries like jQuery.
To convert the JSON string to a javascript object, use JSON.parse(). Here's the reference:
http://developer.mozilla.org/En/Using_native_JSON
Here's an example:
function readJSON(file) {
var request = new XMLHttpRequest();
request.open('GET', file, false);
request.send(null);
if (request.status == 200)
return request.responseText;
};
var myObject = JSON.parse(readJSON('/local/path/to/json'));
EDIT #2: Thanks for editing in this example, Chase. A word of warning. It is not a good idea to make the open() method a synchronous call by using false in the 3rd parm. AJAX is intentionally designed for asynchronous use, and to make a synchronous call invites lock ups. As one who used to think there was a place for synchronous calls, I now find there's always a better way to get it done asynchronously. Word to the wise.
Please have a look at be below code snap which will on in all browsers, thanks
function getJSONData(jsonurl) {
var req = null;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
req.open('GET', jsonurl, false);
req.send(null);
return req.status == 200 ? req.responseText : 'Error occurred';
}
var jsonData = JSON.parse(getJSONData('/local/path/to/json'));
alert(getJSONData('/local/path/to/json'));
Hope this will be very helpful, thanks for your time
I you're familiar with jQuery, this is a drop-in replacement for $.ajax:
Script:
function ajax( uri, settings ) {
var ajax = new window.XMLHttpRequest(),
data = settings.type == 'GET' ? '' : settings.data,
async = settings.async ? settings.async : false;
uri = settings.type == 'GET' ? uri + ( settings.data ? '?' + settings.data : '' ) : uri;
ajax.onreadystatechange = function () {
if ( ajax.readyState == 4 ) { //response ready
if ( ajax.status == 200 ) { //success
if ( settings.success ) settings.success( ajax.responseText, ajax.statusText );
if ( settings.complete ) settings.complete( ajax, ajax.statusText );
} else {
if ( settings.error ) settings.error( ajax, ajax.status, ajax.statusText );
};
};
};
ajax.open( settings.type, uri, async );
if ( settings.headers ) {
for ( var header in settings.headers ) {
ajax.setRequestHeader( header, settings.headers[header] );
};
};
ajax.send( data );
};
Call it just like jQuery:
ajax( '/local/path/to/json', {
"type": "GET", //or "POST"
//"data": "<query string>", //if POST
"success": function ( data, status ) {
var myJson = window.JSON.parse( data );
},
"error": function ( response, status, error ) {
// handle error
}
} );