Transferred from: http://www.cnblogs.com/satng/
We cannot implement the return value of the success method using the Jquery.ajax function, and sometimes we need to process the data that is returned successfully, in general, we will return a lot of data after interacting with the server, and some of the data need to be handled specially, then we need to implement the success method return, but JQ Uery.ajax is not possible, the traditional jquery.ajax is used as follows:
- Jquery.ajax ({
- Type: "POST",
- URL: "some.php",
- Data: "Name=john&location=boston",
- Success:function (msg) {
- return msg;
- }
- });
As above: Return MSG, operation is invalid.
After we interact with the server, we return the data in MSG, but we need to implement a return, so how to solve the problem.
Train of thought, by passing in a function, Jquery.ajax triggers the success event to call this function, and passes the returned data msg to the incoming function, implements the function to the data processing.
To solve this problem, we wrote an example of three files, an HTML file, a JS file, and a PHP file, respectively.
Example analysis: One, JS part
- var biuuu = {};
- Biuuu._ajax = function (Url,data,datatype,_success_func,_error_func) {
- Jquery.ajax ({
- Type: "Post",
- Url:url,
- Data:data,
- Datatype:datatype,
- Success:function (info) {
- _success_func (info);
- },
- Error:function () {
- _error_func ();
- }
- });
- }
Pass in four parameters, namely URL: is the php file processing Ajax submission, as follows: biuuu.php data: Incoming post, as follows: Do=test datatype: Represents the data type, as follows: JSON _success_ Func: The function name that handles the successful operation, as follows: Biuuu.succfunction _error_func: The function name that handles the failed operation, as follows: Biuuu.failfunction
Second, HTML section (trigger event)
- <script type= "Text/javascript" src= "Http://code.jquery.com/jquery.js" ></script>
- <script type= "Text/javascript" src= "Biuuu.js" ></script>
- <script type= "Text/javascript" >
- <!--
- JQuery (document). Ready (function () {
- JQuery ("#Open"). Click (function () {
- Biuuu._ajax ("biuuu.php", ' do=test ', ' json ',biuuu.succfunction,biuuu.failfunction);
- });
- });
- biuuu.succfunction = function (info) {
- alert (info.message);
- }
- biuuu.failfunction = function () {
- Alert (' fail ');
- }
- -
- </script>
- <a href= "#" id= "open" >test</a>
When the test is clicked, the Ajax commit is triggered, the real work calls the Biuuu.succfunction function, and the Biuuu.failfunction function fails to call
Third, the PHP section
- <?php
- $info = Array (' message ' = ' biuuu is success ', ' bool ' = TRUE);
- Print_r ( json_encode ($info));
- Exit ();
- ?>
Returns a JSON data
The workaround is very simple, and the method of passing in the function name solves the problem that the Jquery.ajax cannot implement the return value call.
Click I download if resolve Jquery.ajax cannot implement return value call problem instance
Jquery.ajax cannot implement return value call problem