2013-03-23 52 views
1

我如何獲得在我的控制器中發送?這是什麼,我已經嘗試了:代碼點火器從視圖到控制器獲取ajax值

阿賈克斯

$.ajax({ 
    type: "POST", 
    url: "example/name", 
    data: send, 
    success: function(value) { 

    } 
}); 

控制器

class Example extends CI_Controller { 
    function name() { 
     $this - > post(send); 
    } 
} 
+1

是什麼'send'變量? – dfsq 2013-03-23 08:23:35

+0

@dfsq:它有一個字符串作爲內容。如何發送ajax值並在控制器中進行檢索。 – ram 2013-03-23 08:25:32

+0

你確定控制器上有一個名爲'post'的函數嗎?我希望你需要獲取'POST'值,請使用'$ this-> input-> post('field_name')' – Red 2013-03-23 09:31:37

回答

2

看來你沒有正確發送數據。試試這個:

$.ajax({ 
    type: "POST", 
    url: "example/name", 
    data: {send: send}, 
    success: function(value) { 

    } 
}); 

在這種情況下,你將作爲$_POST['send']

+0

如何在控制器中檢索? – ram 2013-03-23 08:26:25

+0

您可以將其作爲任何其他POST參數獲取。我對Codeigniter並不熟悉,但在文檔中進行快速查找後,似乎您可以將其作爲$ this-> input-> post('send');檢索出來。 – dfsq 2013-03-23 08:27:03

+0

:在我的firebug中,它向我展示了example/index/example/name。爲什麼它是這樣發佈的?我的代碼點火器說錯誤遇到了 您請求的操作是不允許的。 – ram 2013-03-23 08:39:12

0

應指定值來發布與發送,即它應該是喜歡 -

$.ajax({ 
    type: "POST", 
    url: "example/name", 
    data: 'send='+1, 
    success: function(value) { 

    } 
}); 

然後你就會有這種變量的值,你在做什麼。

using-

$this->input->post('send'); 
+0

在我的firebug中,它向我展示了example/index/example/name。爲什麼它是這樣發佈的? – ram 2013-03-23 08:33:35

+0

嘗試從http中使用完整的網址,直到您的功能使用<?php echo base_url();?>在腳本 – 2013-03-23 09:41:22

1

試試這個,這是Ajax調用

$.post('<?php echo base_url()?>example/name',{send:send}, 
      function(data) { 

     }); 

然後使用後訪問到你的控制器,這樣

class Example extends CI_Controller { 
function name() { 
    $_POST['send']; 
    } 
} 
+0

中:現在我的網址結構正確但mu火蟲顯示爲 POST http:// localhost/sample/index .php/example/name 500內部服務器錯誤195ms。當我登陸這個頁面時,我的URL看起來像這樣:http://localhost/sample/index.php/example/data/13 – ram 2013-03-23 09:04:22

1

首先,你可以定義全局變量,可以用作jQuery代碼中的基礎URL。將此放在頁面<script>標籤<head>部分

  //<![CDATA[ 
       base_url = '<?php echo base_url();?>'; 
     //]]> 

比做這樣

 var data = 'var1=aaa&var2=bbb'; 

     $.ajax({ 
      type: "POST", 
      url: base_url+"mainController/getData/", //base_url is the variable which you have defined in the head section 
      data: data, 
      success: function(response){ 
        alert(response); 
      } 
     }); 

比控制器Ajax請求檢索後數據這樣

 class MainController extends CI_Controller { 

      function getData() 
      { 
       $var1 = $this->input->post('var1'); 
       $var2 = $this->input->post('var2'); 

       echo $var1; 
       echo '<br/>'; 
       echo $var2; 
      } 
     } 
相關問題