jquery Ajax request processing and processing of the JSON data in the background Java __JS

Source: Internet
Author: User
General format of $.ajax

$.ajax ({

     type: ' POST ',

     url:url,

    data:data,

    success:success,

    datatype:datatype

});





Ii. Description of $.ajax parameters
Parameter description
URL required. Specify which URL to send the request to.
Data is optional. The mapping or string value. Specify the data to be sent to the server along with the request.
Success (data, Textstatus, JQXHR) is optional. The callback function to execute when the request succeeds.
DataType

Optional. Specify the data type of the expected server response.

The default execution of intelligent judgments (XML, JSON, script, or HTML).
1.$.ajax asynchronous request with JSON data
var AJ = $.ajax ({  
    URL: ' productmanager_reverseupdate ',//Jump to action  
    data:{  
             Selrollback:selrollback,  
             Seloperatorscode:seloperatorscode,  
             provincecode:provincecode,  
             pass2:pass2  
    },  
    type: ' Post ',  
    Cache:false,  
    dataType: ' json ',  
    success:function (data) {  
        if (data.msg = = "true" {  
            //view ("modified successfully.") ");  
            Alert ("modified successfully.") ");  
            Window.location.reload ();  
        } else{  
            View (data.msg);  
        }  
     ,  
     error:function () {  
          //view (Exception). ");  
          Alert ("Exception. ");  
     }  
});




2.$.ajax an asynchronous request that serializes the contents of a table as a string
function Notips () {  
    var Formparam = $ ("#form1"). Serialize ()//Serialization table content is String  
    $.ajax ({  
        type: ' Post ',      
        URL : ' Notice_notipsnotice ',  
        Data:formparam,  
        cache:false,  
        dataType: ' json ',  
        success:function (data) {  
        }  
    });  
}  




Asynchronous request for 3.$.ajax stitching URL
var yz=$.ajax ({  
     type: ' Post ',  
     URL: ' validatepwd2_checkpwd2?password2= ' +password2,  
     data:{},  
     cache: False,  
     DataType: ' json ',  
     success:function (data) {  
          if (data.msg = = "false")//server returns FALSE, To change the value of ValidatePassword2 to Pwd2error, which is asynchronous, you need to consider the return time  
          {  
               textpassword2.html ("<font color= ' red ' > Business password is incorrect.") </font> ");  
               $ ("#validatePassword2"). Val ("Pwd2error");  
               CheckPassword2 = false;  
               return;  
           }  
      ,  
      error:function () {}  




4.$.ajax asynchronous request for splicing data
$.ajax ({   
    URL: ' ${basepath}/jobs/dictionary/post ',   
    type: ' Post ',   
    data: ' Mername= ' +values,   
    async: False,//default is True asynchronous   
    Error:function () {   
       alert (' Error ');   
    },   
    success:function (data) { 
    / Backstage passed is list<map> or list<object>
       var jsonobj=eval ("(" +data+) ");  
        $.each (jsonobj, function (I, item) {  
            //$ ("#taskClass"). Append ("<option value=" "+item.+".) +item.fieldname+ "' >" +item.statudesc+ "</option>");
            Alert (Item.value + ","  + Item.desc);  
        });   
       $ ("#" +divs). HTML (data);   
    }
);




Background action

Json-lib This Java class package is used to convert Bean,map and XML into JSON and to turn JSON back into beans and dynabean.

Download Address: http://json-lib.sourceforge.net/
The 3rd party package is also required:
Org.apache.commons (more than 3.2 versions)
Org.apache.oro
Net.sf.ezmorph (Ezmorph-1.0.4.jar)
Nu.xom
 @RequestMapping (value= "post") public void post (HttpServletRequest request,httpservletresponse R
        Esponse) {list<map<string,string>> listmap = new arraylist<map<string,string>> (); 
             for (int i = 0; i < 6; i++) {map<string,string> Map = new hashmap<string,string> ();
             Map.put ("value", "id" +i);
             Map.put ("desc", i+ "");
        Listmap.add (map);
        } System.out.println (Jsonarray.fromobject (Listmap). toString ()); 

    Stringutil.write (response, Jsonarray.fromobject (Listmap). toString ()); public static void Write (HttpServletResponse response,string message) {try {Response.setcontenttyp
            E ("text/html");
            Response.setcharacterencoding ("Utf-8");
            Response.getwriter (). write (message);
        Response.getwriter (). Flush ();
        catch (IOException e) {e.printstacktrace (); }
    }




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.