简体   繁体   English

在以下情况下如何访问json数据并在if条件下使用?

[英]How to access json data and use in if condition in following scenario?

Following is my AJAX function code: 以下是我的AJAX功能代码:

$('#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. 作为回应,以下是来自PHP的内容。 This content is already converted into json format using the method json_encode() 已经使用json_encode()方法将该内容转换为json格式

{
    "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. 我想检查数组response [error_message]是否为空,然后要在警报框中显示内容,否则什么也不做。

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) ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以下情况下如何访问PHP文件中的序列化数据? - How to access the serialized data in PHP file in the following scenario? 如何访问以下JSON数据中的每个元素? - How to access each element in the following JSON data? 在以下情况下如何在jQuery对话框上使用create事件? - How to use create event on a jQuery dialog in following scenario? 在以下情况下如何在$ .ajax()函数的data属性中发送参数? - How to send a parameter in data attribute of $.ajax() function in following scenario? 在以下情况下如何将数据追加到jQuery中的特定div? - How to append data to a particular div in jQuery in the following scenario? 如何在以下情况下实现自动完成 - How to implement autocomplete for the following scenario 如何访问具有以下格式的json响应 - How to access json response that have the following format 如何在以下情况下将数组从smarty模板分配给javascript,并在javascript中访问相同的分配数组? - How to assign array from smarty template to javascript and access the same assigned array in javascript in following scenario? 在这种情况下如何使用React? - How to use React in this scenario? 在以下情况下如何使div内容居中对齐? - How to align div content to center in following scenario?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM