Ajax calls from php page - checking for empty array as a result

I have a php page that includes the following javascript:

<script> $(document).ready(function(){ $('#search').hide(); }); $('#L1Locations').live('change',function(){ var htmlstring; $selectedvalue = $('#L1Locations').val(); $.ajax({ url:"<?php echo site_url('switches/getbuildings/');?>" + "/" + $selectedvalue, type:'GET', dataType:'json', success: function(returnDataFromController) { alert('success'); var htmlstring; htmlstring="<select name='L2Locations' id='L2Locations'>"; htmlstring = htmlstring + "<option value='all'>All</option>"; console.log(returnDataFromController); var JSONdata=[returnDataFromController]; alert('string length is' + JSONdata.length); if(JSONdata.length > 0) { for(var i=0;i<JSONdata.length;i++){ var obj = JSONdata[i]; for(var key in obj){ var locationkey = key; var locationname = obj[key]; htmlstring = htmlstring + "<option value='" + locationkey + "'>" + locationname + "</option>"; } } $('#l2locations').html(htmlstring); } else { alert('i think undefined'); $('#l2locations').html(''); } } }); $('#search').show(); }); </script> 

what I'm trying to do dynamically shows a combined field if the variable "returnDataFromController" has any elements.

But I think I have an error with a string that checks JSONdata.length.
Regardless of whether or not the ajax call returns with a filled array or empty, the length always indicates the presence of 1. I think I'm confused as to what is counted when you request the length. Or maybe my dataType property is wrong? I'm not sure.

In case this helps you, the string "console.log (returnDataFromController)" gives the following results when I get the data back from the ajax call (and therefore when the combo should be created)

 [16:28:09.904] ({'2.5':"Admin1", '2.10':"Admin2"}) @ http://myserver/myapp/index.php/mycontroller/getbranches:98 

In this scenario, the combo box is displayed with the correct content.

But in the scenario where I return an empty array, a combo box is also created. Here's what disables the .log console:

 [16:26:23.422] [] @ http://myserver/myapp/index.php/mycontroller/getbranches:98 

Can you tell me where I am going wrong?

EDIT:

I understand that I consider my return data as an object - I think I want it because I am returning an array. I guess I need to know how to correctly check the length of an array in javascript. I thought it was just .length.

Thanks.

EDIT 2: Maybe I just need to transfer the results of my controller? Instead of returning an empty array, should I return false or NULL?

  if (isset($buildingforbranch)) { echo json_encode($buildingforbranch); } else { echo json_encode(false); } 

EDIT 3:

Based on a post found in Parse JSON in JavaScript? , I changed the code in the Success section of the ajax call to look like this:

  success: function(returnDataFromController) { var htmlstring; htmlstring="<select name='L2Locations' id='L2Locations'>"; htmlstring = htmlstring + "<option value='all'>All</option>"; console.log(returnDataFromController); var JSONdata=returnDataFromController, obj = JSON && JSON.parse(JSONdata) || $.parseJSON(JSONdata); alert(obj); } 

But I get an error

 [18:34:52.826] SyntaxError: JSON.parse: unexpected character @ http://myserver/myapp/index.php/controller/getbranches:102 

Line 102:

 obj = JSON && JSON.parse(JSONdata) || $.parseJSON(JSONdata); 
0
source share
1 answer

The problem was that the data from the controller was corrupted by JSON.

Pay attention to the part of my post where I show the returned data:

  ({'2.5':"Admin1", '2.10':"Admin2"}) 

2.5 should consist of double quotes, not single quotes. I do not understand how / why this happens, but I will create another post to address this issue. Thanks to all.

0
source

Source: https://habr.com/ru/post/948546/


All Articles