使用 AJAX jQuery 的基本 Qn?
我刚刚开始学习通过 Codeigniter 使用 AJAX。在我看来,我有一个文本区域和一个按钮,该按钮使用 AJAX 将文本区域中的文本提交到我的控制器,控制器从数据库检索数据并将该数据返回到视图。但是我在回调函数中收到错误“不允许的关键字符”。即使我只是echo
一个字符串,也会发生这种情况。怎么了?
顺便说一句,我应该在控制器中使用 return $result
或 echo $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";
}
I just started learning using AJAX with Codeigniter. On my view, I have a textarea and a button which uses AJAX to submit the text in the textarea to my controller, which retrieves data from the database and returns this data to the view. However I am getting the error "disallowed key characters" in the callback function. This happens even when I simply echo
a string. What is happening?
Btw, should I use return $result
or echo $result
in the controller to pass the data back to the webpage?
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);
});
});
});
Controller
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";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeIgniter 将 url 中的字符限制为:
当您发送 AJAX 请求时,您可能会在地址中输入不在此列表中的字符。我的建议是将
$.get
更改为$.post
,然后在控制器中获取发布数据。像这样的东西:AJAX
Controller
至于
echo
与return
,使用echo
。CodeIgniter has restricted the characters in the url to:
Chances are you are putting in characters that not in this list in the address when you send the AJAX request. My suggestion would be change the
$.get
to$.post
and then get the post data out in the controller. Something like this:AJAX
Controller
As for the
echo
vsreturn
, useecho
.您可能不允许在 URL 中使用查询字符串,并且 Ajax 函数会将查询字符串添加到 URL。查看以下网址,了解如何打开查询字符串:
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
You're probably not allowing querystrings in your URL and the Ajax function adds querystring to the url. Take a look at the following url to learn how to turn querystrings on:
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html