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