2012-02-13 102 views
0
相應頁面

的控制器這是我已經包含在HEAD標記自己的JavaScript函數:發送數據通過JSON(AJAX功能),但不能訪問

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

     } 


    }); 

} 

,我想將數據發送到我的PHP控制器在按鈕的Cilck事件上使用此功能。我寫了下面的代碼的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); 

不過,雖然上點擊我沒有得到任何輸出或結果.... 請儘快引導我..... :)

+0

什麼是HTML的樣子。你可以張貼其中的一部分陳述Select1。行動的路徑應該是相對的/測試/?可以這是一個問題 – 2012-02-13 10:44:27

回答

1

存在一些誤用。

首先,你確定GETACTION()獲取/ test路徑嗎?其次,在你的JS代碼中,我不確定你是否正確地調用了正確的路徑。在url參數的開始

$.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); 
    } 
}); 

注意斜線:

你最好使用這個。這意味着:「在域名後面開始」。另外,當您使用jQuery時,您不需要使用詳細的document.getElementById() :)。

最後,請參閱data參數。 jQuery將在發送AJAX請求之前自動構建正確的URL。我認爲它可以讓你的代碼更清潔。

+0

嗨戴維斯,真的thanx你的關注....我已經使用上述代碼提供的你.... !!布現在它的顯示(在控制檯的鉻)「測試沒有定義」....並且以某種方式引導我,以及如何檢查它的輸出..... !! – user1206552 2012-02-13 11:47:17

+0

嗨,我不是戴維斯:)看起來你忘了'/ test'的引號。檢查你的代碼。 – 2012-02-13 12:48:29