来自php页面的ajax调用-检查结果是否为空数组 [英] ajax calls from php page - checking for an empty array as result

查看:109
本文介绍了来自php页面的ajax调用-检查结果是否为空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下javascript的php页面:

I have a php page which 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>

我要完成的操作是动态显示组合框,如果变量 returnDataFromController具有项目。

what i'm trying to accomplish is dynamically show a combo box if the variable "returnDataFromController" has any items.

但是我认为我在检查JSONdata.length的行中存在一个错误。

无论ajax调用是否返回填充的数组还是一个空的,长度总是显示为1。我想知道当您要求长度时该算什么。也许我的dataType属性不正确?我不确定。

But I think I have a bug with the line that checks JSONdata.length.
Regardless of whether or not the ajax call returns a populated array or an empty one, the length always shows a being 1. I think I'm confused as to what is counted when you ask for the length. Or maybe my dataType property is incorrect? I'm not sure.

以防万一,当我确实从ajax调用中获取数据时, console.log(returnDataFromController)行给出以下结果(因此当应当创建组合)

In case it helps you, the "console.log(returnDataFromController)" line gives the following results when i do get data back from the ajax call (and hence 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 contents.

但是在返回空数组的情况下,也会创建组合框。这是console.log调用转储的内容:

But in scenario where I'm returning an empty array, the combo box is also created. Here's what the console.log call dumps out:

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

你能告诉我我要去哪里了吗?

Can you tell me where I'm going wrong?

编辑:

我意识到我将返回数据视为一个对象-我想这就是我想要的,因为我要返回一个数组。
我想我需要知道如何正确检查JavaScript中数组的长度。我以为只是长度。

I realize that I'm treating my return data as an object - I think that's what I want because i'm getting back an array. I guess I need to know how to properly check the length of an array in javascript. I thought it was just .length.

谢谢。

编辑2:
也许我应该改变控制器发送的结果?我应该返回false或NULL而不是返回空数组?

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

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

编辑3:

基于位于在JavaScript中解析JSON的帖子? ,我将ajax调用的成功部分中的代码更改为:

Based on the post found at Parse JSON in JavaScript?, I've changed the code in the "success" section of the ajax call to look like:

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

但是我在

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

第102行是:

obj = JSON && JSON.parse(JSONdata) || $.parseJSON(JSONdata);


推荐答案

问题是来自控制器的数据格式错误JSON。

The problem was that the data from the controller is malformed JSON.

请注意我的帖子中显示返回数据的部分:

Note the part of my post where I show the return data:

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

2.5应该用双引号而不是单引号。
我不知道这种情况如何/为什么发生,但我将创建另一篇文章来处理该问题。感谢大家。

The 2.5 should be in double quotes not single quotes. I don't understand how / why this is happening but I will create another post to deal with that question. Thanks everyone.

这篇关于来自php页面的ajax调用-检查结果是否为空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆