IE中的jQuery $ .get问题

时间:2021-04-07 10:02:05

I have a login form which appears at the top of all of my pages when the user is logged out. My current jQuery/javascript code works in Firefox 3 but not IE 7. The code queries a page which simply returns the string "true" or "false" depending on whether the login was successful or not. Inside my $.ready() function call I have the following...

我有一个登录表单,当用户注销时,该表单显示在我的所有页面的顶部。我当前的jQuery / javascript代码适用于Firefox 3但不适用于IE 7.代码查询一个页面,该页面只返回字符串“true”或“false”,具体取决于登录是否成功。在我的$ .ready()函数调用中,我有以下内容......

$('#login_form').submit(function() {

        var email = $('input#login_email').val();
        var pw = $('input#login_password').val()

        $.get('/user/login.php', { login_email: email, login_password: pw }, function(data) {
            alert('get succeeded');
            if(data == 'true') {
                $('#login_error').hide();
                window.location = '/user/home.php';
                alert('true');
            }
            else {
                $('#login_error').show();
                alert('false');
            }

        });

        alert('called');

        return false;
    });

In FF, I am successfully transferred to the intended page. In IE, however, the below alerts "called" and nothing else. When I refresh the page, I can see that I am logged in so the $.get call is clearly going through, but the callback function doesn't seem like its being called (ie. "get succeeded" is not popping up). I also don't appear to be getting any javascript error messages either.

在FF中,我已成功转移到预期页面。然而,在IE中,下面的警报“被叫”而没有别的。当我刷新页面时,我可以看到我已登录,因此$ .get调用显然正在通过,但回调函数似乎不被调用(即“获得成功”不会弹出)。我也似乎没有收到任何javascript错误消息。

Why isn't this working in IE?

为什么这不适用于IE?

Thanks

EDIT: Since a couple people asked, whenever I enter a correct email/password or an incorrect one, nothing in the callback function happens. If I manually refresh the page after entering a correct one, I am logged in. Otherwise, I am not.

编辑:由于有几个人问,每当我输入正确的电子邮件/密码或不正确的电子邮件/密码时,回调函数中都不会发生任何事情。如果我在输入正确的页面后手动刷新页面,我就会登录。否则,我不是。

EDIT 2: If I alert out data in the callback function nothing happens in IE (I do not get an alert popup). In FF, it alerts true for valid email/pw combos and false for invalid ones. I am using jQuery 1.3.2.

编辑2:如果我在回调函数中提醒数据IE中没有任何反应(我没有得到警告弹出窗口)。在FF中,它对有效的电子邮件/ pw组合警告为真,对于无效的组合则为false。我正在使用jQuery 1.3.2。

EDIT 3: Ok, guys, I tried R. Bemrose's thing down there and I'm getting a "parseerror" on the returned data. I'm simply echoing 'true' or 'false' from the other PHP script. I also tried 'yes' and 'no', but that still gave me a parse error. Also, this works in Chrome in addition to FF.

编辑3:好的,伙计们,我在那里尝试了R. Bemrose的事情,我对返回的数据得到了一个“解析错误”。我只是从其他PHP脚本回显'true'或'false'。我也试过'是'和'不',但这仍然给了我一个解析错误。此外,除了FF之外,这适用于Chrome。

8 个解决方案

#1


In your response type use:

在您的响应类型中使用:

header("content-type:application/xml;charset=utf-8");

#2


As stupid as this sounds... perhaps IE7 is being anal retentive about the missing semicolon on the var pw line?

这听起来很愚蠢......也许IE7正在肛门保留关于var pw线上缺少的分号?

Probably not, but the only way I can think of getting more information is to convert it to an $.ajax call in order to add an error hook and see which error type it think is happening. Oh, and to check out the exception object.

可能不是,但我能想到获取更多信息的唯一方法是将其转换为$ .ajax调用,以便添加错误挂钩并查看它认为发生的错误类型。哦,并检查异常对象。

$.ajax({
        type: 'GET',
        url: '/user/login.php',
        data: { login_email: email, login_password: pw },
        success: function(data) {
                alert('get succeeded');
                if(data == 'true') {
                        $('#login_error').hide();
                        window.location = '/user/home.php';
                        alert('true');
                }
                else {
                        $('#login_error').show();
                        alert('false');
                }
        },
        error: function(xhr, type, exception) {
                alert("Error: " + type);
        }
});

If the error type is parse, IE may be complaining because the data coming back has extra commas at the end of comma separated arrays/lists.

如果错误类型是解析,IE可能会抱怨,因为返回的数据在逗号分隔的数组/列表末尾有额外的逗号。

#3


IE uses cached data for get requests. Maybe that's your problem? What happens if you try different user id, password?

IE使用缓存数据来获取请求。也许那是你的问题?如果您尝试不同的用户ID,密码会怎样?

In any case, isn't it a better idea to send password in POST? :)

无论如何,在POST中发送密码不是一个好主意吗? :)

#4


I'm am having a similar problem with the following code:

我遇到了与以下代码类似的问题:

var listOrder = $(this).sortable('toArray').toString();
var otherVariable = whatever
$.get('process_todo.cfm?method=sortToDos', {listOrder:listOrder,otherVariable :otherVariable });

The variable displays through an alert as 'test_123,test_456'. I can hard code the same values and it does not fail. It must have something to do with the 'toArray'? I have been trying to debug this one thing for hours. Works perfectly in Firefox, Safari and Chrome... of course!

变量通过警报显示为'test_123,test_456'。我可以硬编码相同的值,它不会失败。它必须与'toArray'有关?我一直试图调试这一件事几个小时。在Firefox,Safari和Chrome中完美运行......当然!

#5


Instead of:

if(data == 'true')

if(data =='true')

try:

if(data)

then in your server just return either a 1 (or true) and a empty value.

然后在您的服务器中返回1(或true)和空值。

#6


What you've posted (at least after Edit 2) looks good. I think the problem is in what you haven't posted.

您发布的内容(至少在编辑2之后)看起来不错。我认为问题在于你没有发布的内容。

First, have you checked your server logs to ensure that it's sending back what you presume?

首先,您是否检查过您的服务器日志以确保它发回您的假设?

If so, I'd recommend dropping the submit mechanism and using a 'button' type with an 'onclick' handler, and not 'submit' button w/a 'onsubmit' handler...

如果是这样,我建议删除提交机制并使用带有'onclick'处理程序的'button'类型,而不是使用'onsubmit'处理程序的'submit'按钮...

 <input type="button" id="login_submit" value="Login" />

Then switch the submit handler:

然后切换提交处理程序:

  $('#login_form').submit(function() { ... });

from the form to the button with:

从表单到按钮:

  $('#login_button').click(function() { ... });

If that doesn't help, can you post the HTML for the form, too?

如果这没有帮助,你也可以发布表格的HTML吗?

[Edit 3] - try adding the 4th 'type' parameter of "text" to the $.post() call.

[编辑3] - 尝试将“text”的第4个“type”参数添加到$ .post()调用中。

#7


Have you used Fiddler to have a good look at what's actually being transferred? (http://www.fiddler2.com)

您是否使用过Fiddler来了解实际转移的内容? (http://www.fiddler2.com)

#8


if you are testing/checking your script in local machine then you will not see any thing in any version of internet explorer because IE on localmachine send datatype as text and not xml and in your case again its matter of datatype not compatible with your document datatype so it worth checking if your datatypes are matching

如果你在本地机器上测试/检查你的脚本,那么你将不会在任何版本的Internet Explorer中看到任何东西,因为localmachine上的IE发送数据类型作为文本而不是xml,在你的情况下,它的数据类型与你的文档数据类型不兼容所以值得检查你的数据类型是否匹配

as far as xml goes solution is here http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

至于xml解决方案,请访问http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

you may check this and find some inspiration :)

你可以查看这个并找到一些灵感:)

salman

#1


In your response type use:

在您的响应类型中使用:

header("content-type:application/xml;charset=utf-8");

#2


As stupid as this sounds... perhaps IE7 is being anal retentive about the missing semicolon on the var pw line?

这听起来很愚蠢......也许IE7正在肛门保留关于var pw线上缺少的分号?

Probably not, but the only way I can think of getting more information is to convert it to an $.ajax call in order to add an error hook and see which error type it think is happening. Oh, and to check out the exception object.

可能不是,但我能想到获取更多信息的唯一方法是将其转换为$ .ajax调用,以便添加错误挂钩并查看它认为发生的错误类型。哦,并检查异常对象。

$.ajax({
        type: 'GET',
        url: '/user/login.php',
        data: { login_email: email, login_password: pw },
        success: function(data) {
                alert('get succeeded');
                if(data == 'true') {
                        $('#login_error').hide();
                        window.location = '/user/home.php';
                        alert('true');
                }
                else {
                        $('#login_error').show();
                        alert('false');
                }
        },
        error: function(xhr, type, exception) {
                alert("Error: " + type);
        }
});

If the error type is parse, IE may be complaining because the data coming back has extra commas at the end of comma separated arrays/lists.

如果错误类型是解析,IE可能会抱怨,因为返回的数据在逗号分隔的数组/列表末尾有额外的逗号。

#3


IE uses cached data for get requests. Maybe that's your problem? What happens if you try different user id, password?

IE使用缓存数据来获取请求。也许那是你的问题?如果您尝试不同的用户ID,密码会怎样?

In any case, isn't it a better idea to send password in POST? :)

无论如何,在POST中发送密码不是一个好主意吗? :)

#4


I'm am having a similar problem with the following code:

我遇到了与以下代码类似的问题:

var listOrder = $(this).sortable('toArray').toString();
var otherVariable = whatever
$.get('process_todo.cfm?method=sortToDos', {listOrder:listOrder,otherVariable :otherVariable });

The variable displays through an alert as 'test_123,test_456'. I can hard code the same values and it does not fail. It must have something to do with the 'toArray'? I have been trying to debug this one thing for hours. Works perfectly in Firefox, Safari and Chrome... of course!

变量通过警报显示为'test_123,test_456'。我可以硬编码相同的值,它不会失败。它必须与'toArray'有关?我一直试图调试这一件事几个小时。在Firefox,Safari和Chrome中完美运行......当然!

#5


Instead of:

if(data == 'true')

if(data =='true')

try:

if(data)

then in your server just return either a 1 (or true) and a empty value.

然后在您的服务器中返回1(或true)和空值。

#6


What you've posted (at least after Edit 2) looks good. I think the problem is in what you haven't posted.

您发布的内容(至少在编辑2之后)看起来不错。我认为问题在于你没有发布的内容。

First, have you checked your server logs to ensure that it's sending back what you presume?

首先,您是否检查过您的服务器日志以确保它发回您的假设?

If so, I'd recommend dropping the submit mechanism and using a 'button' type with an 'onclick' handler, and not 'submit' button w/a 'onsubmit' handler...

如果是这样,我建议删除提交机制并使用带有'onclick'处理程序的'button'类型,而不是使用'onsubmit'处理程序的'submit'按钮...

 <input type="button" id="login_submit" value="Login" />

Then switch the submit handler:

然后切换提交处理程序:

  $('#login_form').submit(function() { ... });

from the form to the button with:

从表单到按钮:

  $('#login_button').click(function() { ... });

If that doesn't help, can you post the HTML for the form, too?

如果这没有帮助,你也可以发布表格的HTML吗?

[Edit 3] - try adding the 4th 'type' parameter of "text" to the $.post() call.

[编辑3] - 尝试将“text”的第4个“type”参数添加到$ .post()调用中。

#7


Have you used Fiddler to have a good look at what's actually being transferred? (http://www.fiddler2.com)

您是否使用过Fiddler来了解实际转移的内容? (http://www.fiddler2.com)

#8


if you are testing/checking your script in local machine then you will not see any thing in any version of internet explorer because IE on localmachine send datatype as text and not xml and in your case again its matter of datatype not compatible with your document datatype so it worth checking if your datatypes are matching

如果你在本地机器上测试/检查你的脚本,那么你将不会在任何版本的Internet Explorer中看到任何东西,因为localmachine上的IE发送数据类型作为文本而不是xml,在你的情况下,它的数据类型与你的文档数据类型不兼容所以值得检查你的数据类型是否匹配

as far as xml goes solution is here http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

至于xml解决方案,请访问http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

you may check this and find some inspiration :)

你可以查看这个并找到一些灵感:)

salman