$.post is a method of jquery with the need to introduce jquery.js before use
Syntax: $.post (Url,data,callback,type);
URL (required): address of the sending request, String type
Data (optional): Sent to the background, in key/value form {a:value1,b:value2}, JSON format
Callback (optional): callback function after the request succeeds. Therefore, in the background processing, you need to give Jsonobject put a successful value, see the following example.
Type (optional): That is the data type of the second parameter. If you have data passed to the background, you need to add that type.
Background code:
PublicString Add (User user,httpservletresponse response) {intResulttotal=0//number of records for the OperationResulttotal=userdao.add (user); Jsonobject Resultjsonobject=NewJsonobject (); if(resulttotal>0) {Resultjsonobject.put ("Success",true); }Else{resultjsonobject.put ("Success",false); } response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter (); Out.println (resultjsonobject.tostring); Out.flush (); Out.close (); }
Front Code:
function Add () {var url= "${pagecontext.request.contextpath}/user/add.do"; var username0=$ ("#userName"). Val (); var password0=$ ("#password"). Val (); $.post (Url,{username:username0,password:password0},function (resultjsonobject) { if (resultjsonobject.success { $.messager.alert ("System hint", "add Success", "info"); else{ $.messager.alert ("System hint", "Add Failed", "error") ,} }, "JSON");}
jquery's Post Method $.post ()