douqilin4296
2014-07-19 14:59
浏览 261

Ajax请求返回空数组

I am trying to get the id, where the user clicked, and pass it to my controller via AJAX. For some reason, when i try to show what i am receiving in my controller, it show an empty array.

Any idea why my alert return an empty Array?

JS Function:

function categorySelected(elem) {
    $.ajax({
        url: "/newgallery/creative-fields-click",
        type: "POST",
        data: elem.id
    }).done(function (response) {
        alert(response);
    });
}

PHP:

public function creativeFieldsClickAction()
{
    $idSelected = $this->_request->getPost();
    print_r( $idSelected );
    exit();
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试获取用户点击的id,并通过AJAX将其传递给我的控制器。 某些原因,当我试图在我的控制器中显示我收到的内容时,它会显示一个空数组。

我知道为什么我的警报返回一个空数组? \ n

JS函数:

  function categorySelected(elem){
 $ .ajax({
 url:“/ newgallery / creative-fields-click”,  n type:“POST”,
 data:elem.id 
})。done(function(response){
 alert(response); 
}); 
} 
   
 
 

PHP:

  public function creativeFieldsClickAction()
 {
 $ idSelected = $ this-> _request-> getPost();  
 print_r($ idSelected); 
 exit(); 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjue6584 2014-07-19 15:04
    已采纳

    Data should be JSON, not integer.

    data: { elemid : elem.id }
    

    Then in PHP data can be found from $_POST['elemid']. In your code it could be like

    $this->_request->getPost()['elemid'];
    

    Or something like that.

    已采纳该答案
    打赏 评论
  • doujiao2000 2014-07-19 15:08

    Try by changing

    In ajax

    data: { id : elem.id }
    

    And in action of controller

    $ids =  $_POST;
    print_r($ids);
    
    打赏 评论

相关推荐 更多相似问题