2011-10-18 32 views
0

我剛剛開始學習使用AJAX與Codeigniter。在我看來,我有一個textarea和一個按鈕,它使用AJAX將textarea中的文本提交給我的控制器,該控制器從數據庫中檢索數據並將此數據返回給視圖。但是,我在回調函數中收到錯誤「不允許的關鍵字符」。即使我只是echo一個字符串,也會發生這種情況。發生什麼事?使用AJAX jQuery的基本Qn?

順便說一句,我應該在控制器中使用return $resultecho $result將數據傳回網頁嗎?

AJAX

$(function() { 
    $("#search_button").click(function(e){ 
     e.preventDefault(); 
     var search_location = $("#search_location").val(); 
     $.get('index.php/main/get_places', search_location, function(data){ 
      $("#result").html(data); 
      console.log(data); 
    }); 
}); 

});

控制器

function get_places($address) { 

    $search_latlng = $this->geocode_address($address); 
    $this->load->model('main_model.php'); 
    $result = $this->main_model->get_places($search_latlng); 

    echo "test"; 
} 

回答

0

笨已經在URL中的字符限制爲:

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; //Inside config.php 

機會是你把在不在這個列表中的地址,當你發送的字符AJAX請求。我的建議是將$.get更改爲$.post,然後在控制器中獲取發佈數據。事情是這樣的:

AJAX

$(function() { 
    $("#search_button").click(function(e){ 
     e.preventDefault(); 
     var search_location = $("#search_location").val(); 
     $.post('index.php/main/get_places', {'search_location': search_location}, function(data){ 
      $("#result").html(data); 
      console.log(data); 
     }); 
    }); 
}); 

控制器

function get_places() { 
    $address = $this->input->post('search_location'); 
    $search_latlng = $this->geocode_address($address); 
    $this->load->model('main_model.php'); 
    $result = $this->main_model->get_places($search_latlng); 

    echo $result; 
} 

對於echo VS return,使用echo