无法访问我的帖子值通过发送与jQuery阿贾克斯我的、无法访问、帖子、阿贾克斯

2023-09-10 16:59:56 作者:那寂静的伤

修改:删除的index.php'用的.htaccess创建该probem我刚刚发现。现在我就来解决它。

EDIT: Removing the 'index.php' with .htaccess creates this probem I just discovered. Now I'm on to resolving it.

修改:问题解决了。 JavaScript的是错误的: URL:/登录/它需要一个斜线

EDIT: Problem solved. The javascript was wrong: url: "/login/" It needed a trailing slash.

原始的:在我家的看法,我创建了一个表格:

ORIGINAL: In my home view, I created a form:

<div id="login"><?php 
echo form_open('login');
echo form_input('username', 'Username', 'id="username"');
echo form_submit('submit', 'Login', 'id="login_submit"');
echo form_close();
?></div>

通过一些基本的JavaScript(感谢NETTUTS)我试图执行一些Ajax:

With some basic javascript (thanks to Nettuts) I tried to implement some ajax:

$('#login_submit').click(function() {

var form_data = {
    username: $('#username').val(),
    password: $('#password').val()      
};

$.ajax({
    url: "/login",
    type: 'POST',
    data: form_data,
    success: function(msg) {
        $('#login').html(msg);
    }
});

return false;
});

正如你所看到的,它的形式发送值到登录控制器。

As you can see, it sends the form values to the login controller.

class Login extends CI_Controller {

function index()
{
    if($this->input->is_ajax_request())
    {
        echo '<h2>Login succeeded with ajax</h2>';
    }
    else
    {
        echo '<p>But your ajax failed miserably</p>';
    }
}

}

的问题是,它不工作函数 $这个 - &GT;输入 - &GT; is_ajax_request()输出FALSE。忽略的是,每隔后的数据丢失。 没有被接通。

The problem is, it doesn't work. The function $this->input->is_ajax_request() outputs FALSE. Ignoring that, every other post data is missing. Nothing is put through.

我究竟做错了什么?

推荐答案

我觉得你的问题可能是因为你没有真正发送到正确的脚本。您的网址/登录看起来并不像它会得到一个codeigniter URL(http://domain.com/index.php/login或的 http://domain.com/my_app/index.php/login )。

I think your problem could be that you're not actually sending to the correct script. Your url /login doesn't look like it will get to a Codeigniter URL (http://domain.com/index.php/login or http://domain.com/my_app/index.php/login).

尝试更改URL到控制器或适当的绝对路径或者完整的URL位置。或者:

Try changing the url to either the full url location of the controller or to a proper absolute path. Either:

url: "http://yourdomain.com/index.php/login",

url: "/my_app/index.php/login",

除非你重写的URL,然后/登录可能不会接触到正确的脚本。

Unless you've rewritten the urls then /login probably won't contact the correct script.