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


sending data through JSON(AJAX function) but not able to access the controller of respective page

这是我的JavaScript函数,我已经将其包含在HEAD标签中:

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);

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

有几个误用。

首先,您确定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参数。jQuery会在发送AJAX请求之前自动构建正确的URL。我认为它可以让你的代码更干净。