AJAX不显示结果 [英] AJAX not displaying result

查看:100
本文介绍了AJAX不显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $($ a) document).ready(function(){
$('#category')。change(function(){
var category = $('#category:selected')。 < - 更改此行
console.log(category);
$ .ajax({
type:POST,
url:<?php echo base_url ('Ajax_test_controller / populateSubcategory');?>,
data:category,
dataType:'json',
success:function(data){
console.log data);
$(data).each(function(){
$(#subcategory)。append($& id,
text:this.subcategory,
}));
})
},
错误:function(errorw){
alert hi);
}
});
});
});

这是我的控制器

  public function populateSubcategory(){
$ category_id = $ this-> input-> post('category');
$ data = $ this-> ajax_test_model-> getSubCategory($ category_id);
echo json_encode($ data);
}

这是我的模型

  function getSubCategory($ category_id){
$ this-> db-> select('id,subcategory');
$ this-> db-> from('subcategory');
$ this-> db-> where('category_id',$ category_id);
$ this-> db-> order_by(subcategory,asc);
$ query = $ this-> db-> get();
return $ query-> result();
}

这是我的观点

 < div> 
< div class =form-group>
< label class =control-label>类别:< / label>
< select name =categoryid =category>
< option value =0>选择类别< / option>
<?php
if(is_array($ category)|| is_object($ category)){
foreach($ category as $ category_row){
echo'< option value ='。$ category_row-> id。'>'。 $ category_row-> category。'< / option>';
}
}

?>
< / select>
< label class =control-label>子类别:< / label>
< select name =subcategoryid =subcategory>

< / select>
< / div>
< / div>

我不知道是否有错误的语法,但每当我尝试改变我的模型到

  function getSubCategory($ category_id){
$ cat = 1;
$ this-> db-> select('id,subcategory');
$ this-> db-> from('subcategory');
$ this-> db-> where('category_id',$ cat);
$ this-> db-> order_by(subcategory,asc);
$ query = $ this-> db-> get();
return $ query-> result();
}

我得到这个模型的结果。但这只是为了检查我是否从我的查询中得到任何东西。



我的猜测是category_id不传递给我的模型。但是当我检查category_id的值,它等于1。



我没有看到任何问题。感谢您关于如何解决这个问题的输入。谢谢。

解决方案

使用POST时,从JQuery文档


数据选项可以包含 key1 = value1& key2 = value2 形式的查询字符串,也可以包含 {key1:'value1' key2:'value2'} 。如果使用后一种形式,则在发送数据之前,使用jQuery.param()将数据转换为查询字符串。可以通过将processData设置为false来规避此处理。如果您希望将XML对象发送到服务器,则处理可能不受欢迎;在这种情况下,将contentType选项从application / x-www-form-urlencoded更改为更合适的MIME类型。


Ajax请求尝试这样:

  var category = $('#category:selected')。 

...
...
type:POST,
url:<?php echo base_url('Ajax_test_controller / populateSubcategory')? >,
data:{
category:category
},
// dataType:'json'没有真正理由在这里使用JSON


I'm trying to populate a dropdown from the values of another dropdown so here is my ajax code.

$(document).ready(function() {
    $('#category').change(function () {
    var category = $('#category :selected').val(); // <-- change this line
    console.log(category);
    $.ajax({
        type: "POST",
        url: "<?php echo base_url('Ajax_test_controller/populateSubcategory'); ?>",
        data: category,
        dataType: 'json',
        success: function(data) {
            console.log(data);
            $(data).each(function(){
                $("#subcategory").append($('<option>', {
                    value: this.id,
                    text: this.subcategory,
                }));
            })
        },
        error: function(errorw) {
            alert("hi");
        }
        });
    });
});

This is my controller

public function populateSubcategory(){
    $category_id = $this->input->post('category');
    $data = $this->ajax_test_model->getSubCategory($category_id);
    echo json_encode($data);        
}

This is my model

function getSubCategory($category_id){
    $this->db->select('id, subcategory');
    $this->db->from('subcategory');
    $this->db->where('category_id', $category_id);
    $this->db->order_by("subcategory", "asc");
    $query = $this->db->get();
    return $query->result();
}

This is my view

<div>
    <div class="form-group">
        <label class="control-label">Category : </label>
            <select name="category" id="category">
                <option value="0">Select Category</option>
                <?php
                    if(is_array($category) || is_object($category)){
                        foreach($category as $category_row){
                            echo '<option value="'. $category_row->id .'">'. $category_row->category .'</option>';
                        }
                    }

                ?>
            </select>
        <label class="control-label">Subcategory : </label>
        <select name="subcategory" id="subcategory">

        </select>
    </div>
</div>

I don't know if there is a mistake on my syntax but whenever I try to change my model to

function getSubCategory($category_id){
    $cat = 1;
    $this->db->select('id, subcategory');
    $this->db->from('subcategory');
    $this->db->where('category_id', $cat);
    $this->db->order_by("subcategory", "asc");
    $query = $this->db->get();
    return $query->result();
}

I get the results that I want with this model. But this is just to check if I am getting anything from my query.

My guess is that the category_id is not passed to my model. But when I checked the value of category_id, it is equal to 1.

And I don't see any problem. Thanks for the inputs on how to solve this problem. Thanks.

解决方案

From the JQuery docs, while using POST

The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.

In your Ajax request try this:

    var category = $('#category :selected').val();

...
...
    type: "POST",
    url: "<?php echo base_url('Ajax_test_controller/populateSubcategory'); ?>",
    data: {
    category:category
    },
    //dataType: 'json' No real reason to use JSON here

这篇关于AJAX不显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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