Home >Backend Development >PHP Tutorial >ajax gets the return value method of the background
This article mainly shares with you the method of ajax getting the return value from the background, I hope it can help you.
$.ajax({ type: "POST", url:"http://localhost/.....php", dataType: 'json', async:true, //默认是true异步传输,false是同步传输
data: {"shopid":id,"mem_number":mem_number,"member_id":member_id},
success:function(msg){ // alert("Participation successful! "); // alert(msg); // window.location.reload();//Reload the current page // Ajax is executed asynchronously, and the following code is executed before the data is returned, so neither success nor error can accurately obtain one-to-one return values. // In the end it can only be object object Error:function(msg){ // str = JSON.stringify(msg); // alert(str); // console.log(msg); // alert(msg); // window.location.reload();//Reload the current page Complete:function(msg){//The returned data will be processed accordingly according to the result. This method will be used regardless of whether the request succeeds or fails, so successes and errors are ignored. var str = msg.responseText;//Convert the return result into a string. At this time, the background mark "#" (defined by yourself, mainly used to intercept strings) is mainly used // alert(str); If(str.indexOf("#")>0){//indexOf() method can return the position where a specified string value first appears in the string, if it exists~ alert("Backend query successful") } Else {Alert ("Failure in the background query, please try it later");
} }, }); ajax 直接用.ajax({ type: url: dataType: async: data{"下标1":value1,"下标2":value2,"下标3":value3}, })
That’s it
If you need a background return value, don’t use success, don’t use error
Use complete, because the return value will use this method regardless of success or failure
ajax 直接用.ajax({ type:“” url:“” dataType:‘’ async: data{"下标1":value1,"下标2":value2,"下标3":value3}, complete:function(msg){ var str = msg.responseText; if(str.indexOf("#")>0){ alert("返回后台成功") }else{ alert("返回后台失败") } } })
Backend code
echo "123456123" ;
Returns to a failure to return to the background because no # appears
echo "123456#123" ;
Returns to a successful return to the background because # appears
Related recommendations:
The return value problem of Js
Solution to the return value problem of PHP recursive function
Javascript function parameters and returns Detailed explanation of values and exception codes
The above is the detailed content of ajax gets the return value method of the background. For more information, please follow other related articles on the PHP Chinese website!