【问题标题】:Can't get url parameter无法获取 url 参数
【发布时间】:2015-08-18 03:35:43
【问题描述】:

我使用了带有一个参数月份的 ajax url,这是我从选项框中选择的

  $.ajax({
    url: baseUri + 'attendancelist/search/attsearch/month/' + month ,
    type: 'GET'
    ...
        });

所以我想在我的 attsearchAction() 中调用该参数 所以我这样编码

public function attsearchAction() {        
    $month = $this->request->get('month');  //current testing framework is phalcon
    //$month = $this->_request->getParam('month'); //zend framework is ok by getParam
    var_dump($month);exit;     //null
   }

但它只显示空?怎么解决>>

【问题讨论】:

  • 您是否存储了 Ajax 中使用的变量月份?
  • 我按 id 存储变量 ... //var month = document.getElementById('month').value;

标签: javascript php jquery ajax phalcon


【解决方案1】:

您正在将 Phalcon url 参数 attendancelist/search/attsearch/month/[monthValue] 与 GET 参数 (?month=[monthValue]) 混合。

在 Phalcon 中,您必须设置路由器才能知道 url 的哪一部分是参数。

$router->add(
    "attendancelist/search/:action/month/{month}",
    array(
        "controller" => [your controller],
        "action"     => 1
    )
);

(更多信息请参见the Phalcon Router docs

然后在您的操作中,您必须从调度程序获取参数。

public function attsearchAction() {
    $month = $this->dispatch->getParam('month');
    var_dump($month);exit;
   }

public function attsearchAction($month) {
    var_dump($month);exit;
   }

【讨论】:

  • 如果您要发送 JSON 响应,请记住停用您的视图,$this->view->deactivate()
  • 是的@James Fenwick,但是想调用的 url 来自 jquery,而不是 php phalcon!没有办法了吗,先生!!!!
  • 您必须将您的 jQuery 更改为 @Chayan 的 HTTP GET 样式,或者将您的后端脚本更改为我的答案中的样式,以使其与您现有的类似 REST 的样式一起使用。
【解决方案2】:

如果你想通过url传递参数,你应该使用?来传递参数

url: baseUri + 'attendancelist/search/attsearch/?month=' + month

或者,当您使用 ajax 时,您可以使用 ajax 发送数据。

$.ajax({
    url: baseUri + 'attendancelist/search/attsearch/',
    type: 'GET',
    data: {month: month},
    ...
});

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 2017-10-06
    • 1970-01-01
    • 2017-10-15
    • 2021-05-18
    • 2014-06-13
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多