ExtJS Ajax way back and forth data Transfer (JSON format) __JS

Source: Internet
Author: User

I am also learning ExtJS groping, there is a wrong place, please correct me.

The way ExtJS uses AJAX to transfer data is post (in a regular form or in JSON format), get (with params).

Using JSON (the recommended method) when returning, note that the server must return the JSON format and must set the value of the success, of course you do not, but this way should be officially advocated by the way.

Here is a summary of the JSON data server return method.

In the API always indicates the format of the postback {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. It's proven to be

Success:function (response) {} function, the parameter response is a JSON string that parses the data set by the 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):

Response.Write ' {success:false,data:{msg: ' Password or user name cannot be empty! '}} '

Response.Write "{success:true,data:{msg: ' Login successful! '}}"

Front desk:

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:

Ext.util.JSON.decode (Response.responsetext); This is also the parsing of JSON encoding.

In addition, you can see this code online:

Server:

{success:false, Errors:{errstr: ' Username or password error '}}

Client:

Success:function (form, action) {//response, Options
Location.href = jmp.asp;//Landing successfully after the temporary use of JS to do a jump
},
Failure:function (form, action) {//response, Options
Ext.Msg.alert (' Landing failure ', ACTION.RESULT.ERRORS.ERRSTR);
},

Does not use the known data keyword, in fact, this can also. But note that the Success keyword must be used.!!!!

Here is not very clear, I guess, the previous example is obtained with the first parameter, here is obtained with the second parameter.

the Ext.Ajax.request method is given

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 attributes 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.
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.
Methods String Specifies the method used to send an AJAX request, 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. For XMLHttpRequest, you can query for detailed information by http://www.w3.org/TR/XMLHttpRequest/.
The success function specifies the callback function that executes after 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.
Form object/string Specifies the form ID or form data object to submit.
Isupload Boolean Specifies whether the form to be submitted is a file upload form, which is automatically checked by default.
Headers Object Specifies the header information for the request.
XmlData Object Specifies the XML document to be used to send to the server, and if the property is specified, the parameters set elsewhere are not valid.
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.
Whether the disablecaching Boolean disables cache.

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.