javascript - How to access json data and use in if condition in following scenario? -


following 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 $response[error_message] empty or not             if (!response.error_message)                 alert(response.error_message);          }     });     e.preventdefault(); }); 

here in response following content coming php. content converted json format using method json_encode()

{     "error_message": "id can't blank<br>please select date<br>image can't blank<br>" } 

i want check whether array response[error_message] , if it's not empty want show content in alert box otherwise nothing.

please me in regard.

thanks in advance.

try this:

<script> $('#request_form').submit(function(e) {     //this should 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 $response[error_message] empty or not             if (responseobject.length != undefined && responseobject.length > 0)                 alert(responseobject.error_message);          }     });  });  </script>    

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -