Ajax get back value method in the background

Source: Internet
Author: User
This article is mainly to share with you Ajax to obtain back-end return value method, I hope to help everyone.

$.ajax ({       type: "POST",     URL: "http://localhost/.....php",     dataType: ' json ',     async:true,  // The default is true for asynchronous transfers, false for synchronous transmissions

Data: {"shopid": ID, "Mem_number": Mem_number, "member_id": member_id},

Success:function (msg) {//alert ("Participate successfully! "); Alert (msg); Window.location.reload ();//Reload the current page},//Ajax is executed asynchronously, the data has not returned to execute the following code, so success and error can not accurately get a one-to-one return value// Finally, only Object Object Error:function (msg) {//str = json.stringify (msg);//alert (str);//Console.log (msg);//alert (msg); /Window.location.reload ();//reload Current page}, Complete:function (msg) {///return data according to the results of the corresponding processing, whether the request succeeds or fails, will go this way, So ignore succes and error var str = msg.responsetext;//will return the result to a string, at this time, with the background of the tag "#" (own definition, mainly to intercept the string used) Main//alert (str); The IF (Str.indexof ("#") >0) {//indexof () method returns the position of the first occurrence of a specified string value in the string, or alert ("background query succeeded")}else{alert ("The background query failed, please retry later ");

  }                          }                      ); Ajax directly with. Ajax ({                        type:                        URL:                        dataType:                        async:                        data{"Subscript 1": value1, "Subscript 2": value2, "Subscript 3": Value3},})

You can do it.

If you need to return a value in the background without success without error

Use complete, because the return value regardless of success failed to go this way

Ajax directly with. Ajax ({                        type: ""                        URL: ""                        dataType: '                        async:                        data{"Subscript 1": value1, "Subscript 2": value2, "Subscript 3": VALUE3},                                                    complete:function (msg) {                        var str = msg.responsetext;                         if (Str.indexof ("#") >0) {                                alert ("Return to Background Success")                           }else{                                alert ("Back to Background Failure")}}                        )

Background code

echo "123456123";

Returned back to the background failed because no # appears

echo "123456#123";

Returned back to the background successfully because there are # occurrences

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.