AJAX & JQuery

1.calling a jsp page by passing parameter
$.ajax({  
                url: 'data.jsp?parameter=sahitya',
        async: false,
        error: function() {
        alert("Error");    
         }, 
    success: function(data) {
             alert(data); 
   }
   
});
2.checking weather string contains "/" or not
 if (data.toLowerCase().indexOf("/") >= 0)
    { 
}
3.Redirecting the url to some other
 var path=location.protocol + "//" + location.host+data ;//gives the base path
 window.location=path;
Here 
var pathname = window.location.pathname; // Returns path only 
var url = window.location.href; // Returns full URL
The window.location read-only property returns a Location object with information about the current location of the document.
Ex:
http://www.Sample.com:8082/index.php#tab2?foo=123
Property                                                                        Result
window.location.host                                               www.Sample.com:8082
window.location.hostname                                       www.Sample.com
window.location.port                                                  8082
window.location.protocol                                             http
window.location.pathanme                                        index.php
window.location.href                                   http://www.Sample.com:8082/index.php#tab2
window.location.hash                                                 #tab2
window.location.search                                              ?foo=123
Jquery:

$(location).attr('host');                        

$(location).attr('hostname');                   

$(location).attr('port');                      

$(location).attr('protocol');                   

$(location).attr('pathname');                    

$(location).attr('href');                        

$(location).attr('hash');                      
$(location).attr('search'); 
4.calling Struts1 Action in Ajax 
 $.ajax({url: 'SampleAction.do
                ?method=test
&flagValue='value1'
&flagName='value2',
async: false
5.Difference between success() and complete() in JQuery Ajax.
As of JQuery 1.5 , all JQuery's ajax methods return a jqXHR object that provides .error(),
.success(), and .complete() method.
.success(): only gets called if your webserver responds with a 200k HTTP header --basically when every this is fine
.complete(): always  when the request is complete, no matter the outcome.
Ex:
$.ajax({
Type: "POST",
url: "/test/Sample.do?method=getData",
data: param="value1"&mode="value2",
dataType: "text",
success: function(data){
},
failure: function(){
alert("An error occured. Please Contact Topic team");
},
complete: function(){
}

});
6.Getting JSON type of data by calling url
$.ajax({

url: "/test/Sample.do?method=execute&param1=1&param2='sahitya',
type: "POST",
dataType: "json",
success: function(data){ 
var keys = Object.keys(data); 
},

complete: function(){
alert("completed");
}

});




Comments

Popular posts from this blog

Database Definition and DBMS