使用 AJAX jQuery 的基本 Qn?

发布于 2024-12-10 04:30:59 字数 917 浏览 2 评论 0原文

我刚刚开始学习通过 Codeigniter 使用 AJAX。在我看来,我有一个文本区域和一个按钮,该按钮使用 AJAX 将文本区域中的文本提交到我的控制器,控制器从数据库检索数据并将该数据返回到视图。但是我在回调函数中收到错误“不允许的关键字符”。即使我只是echo一个字符串,也会发生这种情况。怎么了?

顺便说一句,我应该在控制器中使用 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";
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-12-17 04:30:59

CodeIgniter 将 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);
        });
    });
});

Controller

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

至于 echoreturn,使用 echo

CodeIgniter has restricted the characters in the url to:

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

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

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

Controller

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

As for the echo vs return, use echo.

妳是的陽光 2024-12-17 04:30:59

您可能不允许在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文