Jquery.ajax cannot implement return value call problem

Source: Internet
Author: User

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:

    1. Jquery.ajax ({
    2. Type: "POST",
    3. URL: "some.php",
    4. Data: "Name=john&location=boston",
    5. Success:function (msg) {
    6. return msg;
    7. }
    8. });

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

    1. var biuuu = {};
    2. Biuuu._ajax = function (Url,data,datatype,_success_func,_error_func) {
    3. Jquery.ajax ({
    4. Type: "Post",
    5. Url:url,
    6. Data:data,
    7. Datatype:datatype,
    8. Success:function (info) {
    9. _success_func (info);
    10. },
    11. Error:function () {
    12. _error_func ();
    13. }
    14. });
    15. }

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)

    1. <script type= "Text/javascript" src= "Http://code.jquery.com/jquery.js" ></script>
    2. <script type= "Text/javascript" src= "Biuuu.js" ></script>
    3. <script type= "Text/javascript" >
    4. <!--
    5. JQuery (document). Ready (function () {
    6. JQuery ("#Open"). Click (function () {
    7. Biuuu._ajax ("biuuu.php", ' do=test ', ' json ',biuuu.succfunction,biuuu.failfunction);
    8. });
    9. });
    10. biuuu.succfunction = function (info) {
    11. alert (info.message);
    12. }
    13. biuuu.failfunction = function () {
    14. Alert (' fail ');
    15. }
    16. -
    17. </script>
    18. <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

    1. <?php
    2. $info = Array (' message ' = ' biuuu is success ', ' bool ' = TRUE);
    3. Print_r ( json_encode ($info));
    4. Exit ();
    5. ?>

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

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.