javascript - AJAX Success function if there is no returned JSON data -


i know simple struggling right. using jquery/ajax retrieve data (json) database. have success function display data. works fine want have alert user if there no returned results.

my php excerpt encodes json:

...  while($row = $result->fetch_all()) {     $returnresult = $row; } echo json_encode(['result' => $returnresult, 'errors' => $errors]);  .... 

my json data format looks this:

{"result":[["grade 12","studies","john","doe"]],"errors":false} 

my jquery uses json data , displays in html elements:

function getworkload(){          $.ajax({         type: "post",         url: "modules/getworkload.php",         datatype: 'json',         data: {id:id},         cache: false,         })         .success(function(response) {             if(!response.errors && response.result) {                 $.each(response.result, function( index, value) {                     $("#divwork").append('<li class="list-group-item"><b>'+value[0]+'</b> : '+value[1]+'</li>');                     $("#spnname").html('<b>'+value[2]+' '+value[3]+' workload </b>');                 });             } else {                 $.each(response.errors, function( index, value) {                     $('input[name*='+index+']').addclass('error').after('<div class="errormessage">'+value+'</div>')                 });             }         }); } 

i have tried replace if(!response.errors && response.result) { following:

  1. if(!response.errors && response.result=null) {
  2. if(!response.errors && !response.result) {
  3. if(!response.errors && response.result.length < 1) {

your code not correctly parsing json value php file javascript , have parse json before using in javascript

  function getworkload(){          $.ajax({         type: "post",         url: "modules/getworkload.php",         datatype: 'json',         data: {id:id},         cache: false,         })         .success(function(response) {             console.log(response);              var  res = json.parse(response);            console.log(res);/* console json val*/             if(!res.errors && res .result) {                 $.each(res.result, function( index, value) {                     $("#divwork").append('<li class="list-group-item"><b>'+value[0]+'</b> : '+value[1]+'</li>');                     $("#spnname").html('<b>'+value[2]+' '+value[3]+' workload </b>');                 });             } else {                 $.each(res.errors, function( index, value) {                     $('input[name*='+index+']').addclass('error').after('<div class="errormessage">'+value+'</div>')                 });             }         }); } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -