2013-10-23 35 views
-4

我是新來的「jQuery ajax」,我有以下代碼。使用jquery ajax從服務器訪問數據

你能解釋一下我的代碼逐行:

 function fun1() { 
      $.ajaxSetup({jsonp: null,jsonpCallback: null }); 

      $.ajax({ 
      type:'GET', 
      url: 'login.action', 
      dataType: 'json', 
      data: { 
       age:$("#id2").val() 
      }, 
      success: function(data) { 
       printStudentDetails(data); 
      }, 
      error:function() { 
       alert("failure"); 
      } 
     }); 
     } 
+0

難道你只是閱讀[文檔](http://api.jquery.com/jQuery.ajax/)? – George

回答

0
  $.ajax({    // Beginning of your ajax call 
     type:'GET',    // Type can be Get Or Post ie. Your Http Methods 
     url: 'login.action',  // This is the Url Which will be called by the ajax it can be your function at server side 
    dataType: 'json',   // This is the datatype you are expecting from server in this case its json 
    data: { age:$("#id2").val() // This is the data you are passing to your url/function 


     }, 

    success: function(data){ printStudentDetails(data); // This is the callback function which will get execute after the success of ajax call 

        }, 
    error:function() // This is the callback function which will get execute if there is some error is proccesing your request 
     { 
      alert("failure"); 
      } 
    }); 

獲取更多信息:http://api.jquery.com/jQuery.ajax/

0

以下是完整的描述:

$.ajax({  //this line is used to call the ajax 
     type:'GET', //this is used to define the method either get or post 
     url: 'login.action', //here define the url here you want to send the data 
     dataType: 'json', //this is datatype to tell what type of data will return values are text,html,json,xml 
     data: {'age':$("#id2").val()}, //there define the variable and value of that variable which will send to that page which is define in the url 
     success: function(data){ 
     //call when responce is successful 
     printStudentDetails(data); 
     }, 
     error:function() 
     { 
     //call when responce has some error 
     alert("failure"); 
     } 
    }); 
+0

請求從哪個語句發送到客戶端的服務器。 – reddy

+0

@reddy它會在執行時自動發送。您可以在發送請求前使用函數'beforeSend'來設置或執行一些操作。 –