使用JQuery / CodeIgniter调用AJAX后评估返回的数据

时间:2021-11-30 08:01:10

EDIT:

编辑:

I found the problem with my evaluation not working. All the text being returned from my codeIgniter functions has a space before it, so while I saw "success" it was actually " success". I don't know why it is, but I can certainly work with that.

我发现我的评估问题不起作用。从我的codeIgniter函数返回的所有文本都有一个空格,所以当我看到“成功”时,它实际上是“成功”。我不知道为什么会这样,但我当然可以解决这个问题。

As for the next step - opening a new view - Donny's answer was perfect!

至于下一步 - 打开一个新观点 - 唐尼的答案是完美的!


I am using CodeIgniter for an application with JQuery for my AJAX library. I'm just learning the AJAX stuff, so I'm probably missing something basic here...

我正在使用CodeIgniter为我的AJAX库使用JQuery的应用程序。我只是在学习AJAX的东西,所以我可能会遗漏一些基本的东西......

The following code if for a login form.

以下代码适用于登录表单。

The goal is this -use an ajax call on the form submit so I can validate the errors and provide error messages on screen without web page refreshes. I'm doing all my validation with the CodeIgniter form_validation class.

目标是这样 - 在表单提交上使用ajax调用,这样我就可以验证错误并在屏幕上提供错误消息而无需网页刷新。我正在使用CodeIgniter form_validation类进行所有验证。

My codeigniter function returns a text value - either an appropriate error message or the word "success." I want to evaluate the text value, and if it says "success", call another ajax function to load the needed CodeIgniter function that will load home page for logged in users.

我的codeigniter函数返回一个文本值 - 相应的错误消息或“成功”一词。我想评估文本值,如果它显示“成功”,则调用另一个ajax函数来加载所需的CodeIgniter函数,该函数将为登录用户加载主页。

Right now everything works in the code below until I get to the statement "if data=='success'".

现在一切都在下面的代码中工作,直到我得到声明“如果数据=='成功'”。

Why would that return false when I know it is true because the on screen message displays "success"?

当我知道它是真的时,为什么会返回false,因为屏幕上的消息显示“成功”?

$(document).ready(function() {
    $('#registration_form').submit(function(e) {
        e.preventDefault();
        $.post("login", {
            email : $('#email').val(),
            password : $('#password').val()
        }, function(data) {
            $('#message').text(data);

            if (data == "success") {
                alert(data);
                post("landingpage");
            }
        });
    });
});

1 个解决方案

#1


1  

You should consider using jQuery form plugins to do form submit using AJAX.

您应该考虑使用jQuery表单插件来使用AJAX进行表单提交。

To do redirection, use this code:

要进行重定向,请使用以下代码:

window.top.location.assign(URL);

URL is the page destination. If you use CI, you can put this in view:

URL是页面目标。如果您使用CI,则可以将其放入视图中:

window.top.location.assign("<?php echo site_url(THE_PAGE_URL); ");

also, you can examine the data value in the Firebug net tab, or in Web Inspector Resource tab (Safari and Google Chrome).

此外,您可以在Firebug网络选项卡或Web Inspector资源选项卡(Safari和Google Chrome)中检查数据值。

#1


1  

You should consider using jQuery form plugins to do form submit using AJAX.

您应该考虑使用jQuery表单插件来使用AJAX进行表单提交。

To do redirection, use this code:

要进行重定向,请使用以下代码:

window.top.location.assign(URL);

URL is the page destination. If you use CI, you can put this in view:

URL是页面目标。如果您使用CI,则可以将其放入视图中:

window.top.location.assign("<?php echo site_url(THE_PAGE_URL); ");

also, you can examine the data value in the Firebug net tab, or in Web Inspector Resource tab (Safari and Google Chrome).

此外,您可以在Firebug网络选项卡或Web Inspector资源选项卡(Safari和Google Chrome)中检查数据值。