通过JSON(AJAX功能)发送数据但无法访问相应页面的控制器

时间:2012-02-13 10:39:20

标签: php ajax json

这是我在HEAD标签中包含的JavaScript函数:

function test()
{

//enter code here
$.ajax({
      url: 'test?msgto='+document.getElementById('Select1').value,
      dataType: 'json',
      type : 'GET',
      success: function(result)
      {
        //  console.log(result);
          alert('test1');
        //  alert(string(result[0]['Select1']) );

        //  alert(result[0]['TextArea1']);
           //document.getElementById('Text1').Value = ;// string(result[0]['location']);

      }


    });

 }

我希望在按钮的Cilck事件上使用此函数将数据发送到我的PHP控制器。我为GETACTION()

编写了以下代码
    // TODO Auto-generated method stub
    //echo('Get');
    $msgfrom = 1;                    //$this->_getparam('usrid');
    $msgto = $this->_getparam('msgto');

    $sql =  "call select_messages(".$msgfrom.",".$msgto.");";
    $data = Zend_Db_Table::getDefaultAdapter()->query($sql)->fetchAll();
    $this->_helper->json($data);

但在点击时我没有得到任何输出或结果.... 请尽快引导我.......)

1 个答案:

答案 0 :(得分:1)

有几次误用。

首先,您确定GETACTION()检索/ test路径吗?

其次,在你的JS代码中,我不确定你是否正在调用正确的路径。

你最好用这个:

$.ajax({
  type: 'GET',
  url: '/test', // Notice the '/' at the beginning
  data: {
    msgto: $('#Select1').val(), // Since you're using jQuery, do it all the way :)
  }
  success: function(result) {
    alert(result);
  }
});

注意url参数开头的斜杠。这意味着:“从域名开始”。此外,当您使用jQuery时,不需要使用详细document.getElementById()。)。

最后,请参阅data参数。在发送AJAX请求之前,jQuery将自动构建正确的URL。我认为它可以让你的代码更清晰。