Ext. Ajax class
The Ext. Ajax class is a simple and clear xhr package that allows you to execute Ajax requests quickly and effectively. In this tutorial, we will discuss in addition to the public Method Request (), how to use the plaintext message (cleat text) or a powerful extension method to decode JSON objects.
Configuration item object
All files have been archived to Ext. Ajax class Doc
Configuration item
Type
URL |
Character Type |
Required |
Params |
JSON encoded object |
Optional |
Method |
'Get' or 'post' |
Optional |
Success |
Anonymous function object or declared function |
Required |
Failure |
Anonymous function object or declared function |
Required |
Timeout |
Xhr timeout in milliseconds |
Optional |
Conditions for Success and Failure
Success and failure are input by the config parameter config. Simply put, we will call the firstResultAnd the secondRequest.
Attributes generated by the result object
Result. responsetextThe plaintext message returned by the Web server based on the response. If you have control over 100% of the returned text and the data is in JSON format, you should use the Ext. util. JSON. Decode () function to decode the object. Individuals prefer to return JSON data from the server.
If the response of the server is automatically detected by the class,Result. responsexmlMostly used to contain valid XML data.
Request Object Attributes
When different actions are executed, all listed attributes are used. At this time, I do not need to use these attributes.
Simple Example
The following example will execute a request and complete Ext. MessageBox. Alert
Javascript:
Ext.Ajax.request({
url : 'ajax.php' ,
params : { action : 'getDate' },
method: 'GET',
success: function ( result, request ) {
Ext.MessageBox.alert('Success', 'Data return from the server: '+
result.responseText);
},
failure: function ( result, request) {
Ext.MessageBox.alert('Failed', 'Successfully posted form: '+action.date);
}
});
PHP server:
// ajax.php
<?php
if ($_REQUEST['action'] == 'getDate') {
echo date('l dS \of F Y h:i:s A');
}
?>
Advanced example-convert. responsetext results to JSON
HTML + Javascript
<Div>
Here is a simple request.
</Div>
<Div id = "subbutton"> </div>
<Textarea id = "log" Cols = "40" rows = "10"> </textarea>
<SCRIPT type = "text/JavaScript">
FunctionDojson (stringdata ){
Try{
VaRJsondata = ext. util. JSON. Decode (stringdata );
Ext. MessageBox. Alert ('success', 'decodeof stringdata OK <br/> jsondata. Date = '+ jsondata. date );
}
Catch(ERR ){
Ext. MessageBox. Alert ('error', 'Could not decode' + stringdata );
}
}
FunctionDoajax (){
Ext. Ajax. Request ({
URL: 'ajax. php ',
Params: {Action: 'getdate '},
Method: 'get ',
Success:Function(Result, request ){
VaRTextarea = ext. Get ('log'). Dom;
Textarea. Value + = result. responsetext +"\ N";
// Ext. MessageBox. Alert ('success', 'Data return from the server: '+ result. responsetext );
Dojson (result. responsetext );
},
Failure:Function(Result, request ){
Ext. MessageBox. Alert ('failed', 'successfully posted form: '+ result. date );
}
});
}
VaRButton =NewExt. Button ('subbutton ',{
Text: 'click to submit an Ajax request ',
Handler: doajax
});
</SCRIPT>
PHP server
<?
if ($_REQUEST['action'] == 'getDate') {
echo "{date: '" . date('l dS \of F Y h:i:s A') . "'}";
}
?>
-- Djliquidice, 3 August 2007 (CDT)