Ajax呼叫响应未显示

时间:2015-02-03 20:04:28

标签: javascript php ajax json

我正在尝试动态填充带有ajax请求的表单中的select字段到我的基于php的应用程序。下面的代码显示了view / form javascript代码中的ajax请求

$(document).ready(function (){
    var clientDropDown = {
            load: function()
            {
                $.ajax({
                    'type': "post",
                    'url': "http://localhost/promptMe/index.php/main/fetchNames",
                    'data': "client",
                    'delay': 5000,
                    'success': function(result)
                    {
                        clientDropDown.attach(result);
                    },
                    'error': function()
                    {
                        alert("There's a problem!");
                    }
                });
            },
            attach: function(result)
            {
                result = $.parseJSON(result);
                $.each(result,function(id,obj){
                    var client = document.createElement('option');
                    client.setAttribute('value',obj.name);
                    client.text = obj.name;
                    $('select#client_name')[0].add(client);
                });
            }
    };
    clientDropDown.load();

});

在我的控制器中处理此请求的功能如下所示

function fetchNames()
    {
        $entity_type = $this->input->post('data');
        $names = $this->model1->getEntityNames($entity_type);
        print json_encode($names);
    }

在Chrome浏览器开发者工具中,我看到成功的响应,json对象位于渲染视图的顶部,并且没有任何“名称”附加到我的html中的select元素,其中包含下面的代码段

<form method="post" action="receiveEntry" class="form">
<fieldset>
    <legend class="content-header">
        Job Details
    </legend>
    <div class="form-content">
        <div class="form">
            <label for="date">Date</label>
            <input type="date" placeholder="Job Date" id="date" name="date" title="Date Job was prepared" />
        </div>
        <div class="form">
            <label for="client_name">Client Name</label>
            <select name="client_name" id="client_name">

            </select>
        </div>
        <div class="form">
            <label for="description">Description</label>
            <textarea id="description" name="description" title="Description of Estimated Job" cols="40" rows="5" title="Description of job">Job Description</textarea>
        </div>....lots of other unnecessary form fields</fieldset></form>

我正在使用jQuery1.7.2。

请告诉我为什么我不能用返回的json对象的元素填充我的选择字段

0 个答案:

没有答案
相关问题