The method by which the JSON data server is returned.
In the API always indicates the return format
The code is as follows |
Copy Code |
{success;true,data:{clientname: "Fred." Olsen Lines ", portofloading:" Fxt ", Portofdischarge:" OSL "}}} |
Note that the success and data here are the keywords, (let's call it) in data is the information in the client through
Ext. Decode (Response.responsetext). ClientName obtained. The first postback data. Here's the proof.
Success:function (response) {} function, the parameter response is a JSON string that parses the data set through ext. decode.
Callback when successful, the first argument is the XMLHttpRequest object, and the second argument passes in the specified options
All, the code in front and back here should be this uniform:
Background (ASP):
The code is as follows |
Copy Code |
Response.Write ' {success:false,data:{msg: ' Password or user name cannot be empty! '}} ' Response.Write "{success:true,data:{msg: ' Login successful! '}}" |
Front desk:
The code is as follows |
Copy Code |
Success:function (response,options) {Ext. Messagebox.alert (' Hint ', Ext.decode (Response.responsetext). msg); Failure:function (response,options) {Ext. Messagebox.alert (' Hint ', ' Ext.decode (response.responsetext). msg);}, |
It's OK:
The code is as follows |
Copy Code |
Ext. util. Json.decode (Response.responsetext); This |
One is also parsing the JSON encoding
Ext.Ajax.request method
The code is as follows |
Copy Code |
<span style= "Font-size:medium" >ext.ajax.request ({ URL: ' Findbyid.action ', params:{ Id:cell.getId () }, Success:function (resp,opts) { var resptext = Ext.util.JSON.decode (Resp.responsetext); Name=resptext.name; Oid=resptext.id; FindByID (Graph,cell,oid,name); Ext.Msg.alert (' Error ', resptext.name+ "=====" +resptext.id); }, Failure:function (resp,opts) { var resptext = Ext.util.JSON.decode (Resp.responsetext); Ext.Msg.alert (' Error ', resptext.error); } });</span> |
In the Ext development process, basically always need to use asynchronous request, then how does this request actually execute, we next discuss
First: The Ext.ajax class inherits Ext.data.Connection, providing the most flexible way to operate on AJAX requests
Again simple request based on we add a use of the
Indicates that such a request is usually placed in the processing method that triggers an event.
URL: Is the path we want to request
Params: The parameters in the inside are separated by commas, which is the parameter we want to send to the request band
Success: is the server processing successful return
Failure: is server processing failed to return
The key point is how to handle the return value information, our resp This parameter is very important
RESP is a return result object of the EXT construct, such as server-side return "This is a test! "(You can pass throw new Exception (" This is a test! ") Simple implementation). Then the return will be
The following content:
code is as follows |
copy code |
<span style = "Font-size:medium" >tid.1 status.200 Statustext.ok getresponseheader.[ Object Object] getallresponseheaders.server:apache-coyote/1.1 CONTENT-TYPE:TEXT/HTML;CHARSET=GBK CONTENT-LANGUAGE:ZH-CN content-length:108 date:wed, to Oct 2007 12:51:23 GMT responsetext. <title> error </title> <body> </body> Responsexml. Argument.undefined</span> | /tr>
From the above results can be seen, the beginning is a number of state attributes, we are not commonly used, regardless of him. Inside is really commonly used is the responsetext and responsexml two attributes, then this inside responsetext content again by ext uses the HTML to wrap, However, it is appropriate to display it using Ext.messagebox; Reponsexml will be used when the "Text/xml" type is returned on the server side. If the server-side return is a "Text/json" type, the client needs to use obj= Ext.util.JSON.decode (result.responsetext), construct the JSON object, and then use the
Return value for specific operations we write that in JSON.
Servletactioncontext.getresponse (). setContentType ("Text/json; Charset=utf-8 ");
Servletactioncontext.getresponse (). Getwriter (). Write ("{success:true,info: ' Update information succeeded ', Name: ' + oo.getname () +" ', ID: ' " + ID + "'}");
Obviously I'm returning the value of JSON (remember that the attribute value must be enclosed in single quotes)
var resptext = Ext.util.JSON.decode (Resp.responsetext);
This can be returned to the result object, to use the properties of the words resptext.id can be used directly
Using the Ext.Ajax.request method, this method can be used to send an HTTP request to the server side and to handle the returned results in the callback function. Send an HTTP request to the remote server, and the signature of the method when sending the AJAX call is as follows:
Ext.Ajax.rquest ([Object options]): number
The response of the
Server is asynchronous, so the data returned by the server side needs to be processed in the callback function. The callback function can be defined in the parameter options of the request method call. In addition, some other attributes of the AJAX request can be defined in the request method. The parameter options is an object that contains the various parameters and callback processing parameters required by the AJAX request. The various properties and meanings that can be included in the options are as follows: The
URL String Specifies the server-side URL to request, and the default value is the URL parameter value configured in the Ajax object. The
params object/string/function specifies the parameter to pass, either an object containing the parameter name and value, or a URL-encoded string similar to Name=xx&birthday=1978-1-1. Or a function that can return both of these two things. The
Method String specifies how to send the AJAX request using, either get or post. By default, a get is used if no parameters are passed in the request, or a post is used. The
callback function specifies the callback function for the AJAX request, which is executed whether the call succeeds or fails. There are three parameters passed to the callback function, the first option represents the parameters when the request method is executed, the second success indicates the success, and the third parameter response represents the XMLHttpRequest object used to execute the AJAX request. About XMLHttpRequest You can use
to query for detailed information.
The
Success function Specifies the callback function that is executed when the AJAX request executes successfully, passing the two arguments to the callback function, the first parameter response represents the Xmlhttprequet object that executes the AJAX request. The second parameter represents the options object when the request method is executed. The
Failure function specifies the callback function that executes when an error is requested, passed to two parameters of the callback function, and the first parameter response represents the Xmlhttprequet object that executes the AJAX request. The second parameter represents the options object when the request method is executed. The
Scope Object specifies the scope of the callback function, which defaults to browser window. The
form object/string specifies the form ID or form data object to submit. The
Isupload Boolean Specifies whether the form to be submitted is a file upload form, which is automatically checked by default. The
Headers Object specifies the header information for the request. The
XmlData Object specifies the XML document to send to the server, and if specified, the parameters set elsewhere are not valid. The
Jsondata object/string specifies the JSON data that needs to be sent to the server side. If this property is specified, the value of the parameter to be sent elsewhere is set to be invalid.
disablecaching Boolean does not disable cache