How to check a php response for string with jQuery
I have an ajax query sendi开发者_如何学运维ng to a php script and putting the response into a div. The code is as follows:
$.ajax({
type: "POST",
url: "core/process.php",
data: data,
success: function(response){
$("#form").fadeOut('slow');
alert("DO NOT LEAVE THIS PAGE UNTIL COMPLETE");
$("#response").html("DO NOT LEAVE THIS PAGE UNTIL COMPLETE<br />")
.append(response)
.hide()
.slideDown('slow');
}
});
What I want to do is check the response for a string or return such as "true" or "success" or whatever and alert the user the script is complete. I know very little jQuery beyond what you see above and am not sure how to search a response for something specific.
Edit: I just realized I forgot to put how the script outputs the response. It's just echoing in the php script. I'm thinking I may need to put everything into an array and json_encode it, but that's just a guess.
You could use the indexOf
function:
if (response.indexOf('success') > -1 || response.indexOf('true') > -1) {
// the response contains one or both of the strings success and true
}
if (/Success/.test(response)) {
alert('Cool')
}
Or change the response type to json and have the server encode a status code and output string as the result object.
精彩评论