Following is my AJAX function code:
$('#request_form').submit(function(e) {
var form = $(this);
var formdata = false;
if (window.FormData) {
formdata = new FormData(form[0]);
}
var formAction = form.attr('action');
$.ajax({
url : 'xyz.php',
type : 'POST',
cache : false,
data : formdata ? formdata : form.serialize(),
contentType : false,
processData : false,
success: function(response) {
//Here I'm facing issue in checking whether the $response[error_message] is empty or not
if (!response.error_message)
alert(response.error_message);
}
});
e.preventDefault();
});
Here in response following is the content coming from PHP. This content is already converted into json format using the method json_encode()
{
"error_message": "Id can't be blank<br>Please select Date<br>Image can't be blank<br>"
}
I want to check whether the array response[error_message] and if it's not empty then I want to show the content in alert box otherwise do nothing.
Please help me in this regard.
thanks in advance.
Try this:
<script>
$('#request_form').submit(function(e) {
//This should be the first line
e.preventDefault();
var form = $(this);
var formdata = false;
if (window.FormData) {
formdata = new FormData(form[0]);
}
var formAction = form.attr('action');
$.ajax({
url : 'xyz.php',
type : 'POST',
cache : false,
data : formdata ? formdata : form.serialize(),
contentType : false,
processData : false,
success: function(response) {
var responseObject = $.parseJSON(response);
//Here I'm facing issue in checking whether the $response[error_message] is empty or not
if (responseObject.length != undefined && responseObject.length > 0)
alert(responseObject.error_message);
}
});
});
</script>
Use :
if ( response.hasOwnProperty('error_message') && '' != response.error_message ){
alert(response.error_message) ;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.