Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 如何用另一个列表刷新选择列表_Ajax_List_Cakephp_Select - Fatal编程技术网

Ajax 如何用另一个列表刷新选择列表

Ajax 如何用另一个列表刷新选择列表,ajax,list,cakephp,select,Ajax,List,Cakephp,Select,在选择第二个选项后,我正在努力刷新列表。 我有这个清单 <select id="ArticleShopId"> <option>Some options</option> <option>Some options 2</option> <option>Some options 3</option> </select> 我不希望创建一个ajax代码来实现它。然后我试着去做 $('select#Arti

在选择第二个选项后,我正在努力刷新列表。 我有这个清单

<select id="ArticleShopId">
<option>Some options</option>
<option>Some options 2</option>
<option>Some options 3</option>
</select>
我不希望创建一个ajax代码来实现它。然后我试着去做

$('select#ArticleShopId').on('change',function(){
    //alert($(this).val());
    //alert("/articles/refreshCategoriesAjax/"+$(this).val());
    $.ajax({
      type: "GET",
      url: "<?php echo $this->Html->url(array('controller' => 'articles', 'action' => 'refreshCategoriesAjax', 'admin' => true)); ?>",
      data: "id="+$(this).val(),
      success: function(msg){
        console.log(msg);
      }

    })

})
但msg不会返回带有$categories值的数组。 我如何才能正确地调用我的action admin_refreshCategoriesAjax并用$categories的值更新我的第二次选择

非常感谢你的帮助,我花了半天时间:哦

注: 如果我在我的URL中输入这个

http://localhost:8888/web/admin/articles/refreshCategoriesAjax/1


它很好地返回了我所寻找的阵列。如果我用2替换1,它将返回我的其他值。然后这一部分会很好地工作

我相信您的问题在于,在函数admin\u refreshCategoriesAjax中,您返回数组,而不是执行实际将其输出到网页的操作。您可以对$categories进行echo json_编码;要获得一些输出,无需为该操作显式创建视图即可使用。

您将在$this->request->data中找到id,而不是在参数中查找id。要使ajax请求安全,请在视图文件中使用以下代码:

$('select#ArticleShopId').on('change',function(){
     //alert($(this).val());
     //alert("/articles/refreshCategoriesAjax/"+$(this).val());
     $.ajax({
          type: "POST",
          url: "<?php echo $this->Html->url(array('controller' => 'articles', 'action' => 'refreshCategoriesAjax', 'admin' => true)); ?>",
          data: {id:$(this).val()},
          success: function(msg){
               console.log(msg);
          }
     });
});

现在要填充下拉列表中的数据,请从控制器返回json_encode$categories。并使用var result=$.parseJSONmsg;在您的ajax成功方法中。

此ajax是由php生成的还是此js代码位于自定义js文件中?因为你的网址不对。太好了,我稍后会试试这个@用户1395480那么你不接受任何答案吗?
$('select#ArticleShopId').on('change',function(){
    //alert($(this).val());
    //alert("/articles/refreshCategoriesAjax/"+$(this).val());
    $.ajax({
      type: "GET",
      url: "<?php echo $this->Html->url(array('controller' => 'articles', 'action' => 'refreshCategoriesAjax', 'admin' => true)); ?>",
      data: "id="+$(this).val(),
      success: function(msg){
        console.log(msg);
      }

    })

})
$('select#ArticleShopId').on('change',function(){
     //alert($(this).val());
     //alert("/articles/refreshCategoriesAjax/"+$(this).val());
     $.ajax({
          type: "POST",
          url: "<?php echo $this->Html->url(array('controller' => 'articles', 'action' => 'refreshCategoriesAjax', 'admin' => true)); ?>",
          data: {id:$(this).val()},
          success: function(msg){
               console.log(msg);
          }
     });
});
function admin_refreshCategoriesAjax(){
   $categories = array();
   if($this->request->is('post'))
   {
     $id = $this->request->data['id'];
     $this->loadModel('Category');
     // Le list recupere la valeur des IDs et cherche un champs qui a la valeur "name"
     $categories = $this->Category->find('list',array('order'=>'name ASC','conditions'=>array('shop_id'=>$id)));
     //return "toto";
   }
     return $categories;
     #return  json_encode($categories);
}