2011-03-29 95 views
0

EDITED为什么我的jQuery代码不能在Firefox和Chrome中使用?

嗨,我使用masterpage。我在我的项目中使用了formsauthentication。我从SQL Server 2005中提取数据。然后在login.aspx中,我从jQuery中调用我的pagemethod。毕竟,我在IE9.0,Chrome和Firefox中运行我的项目。该项目是正确的。但是这个jQuery代码只能用于IE9.0。

我PageMethod的,被命名为login服务,返回“0”或RETURNURL像“用户/ Default.aspx的”,那么我控制这个塔赫如果login服务的回报是不是“0”的成功将运行以下命令:

alert("there isnt error: " + msg.d); 

,但如果有一个错误,这将运行:

alert("there is error: " + msg.d); 

这是非常有趣的是,

,如果我在IE9运行这个项目,像显示消息“心不是埃罗R:用户/ Default.aspx的”

但是,

如果我运行Chrome或Firefox这个项目,像显示消息‘有错误:未定义’

我怎么可以在所有运行该项目浏览器?

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    jQuery("#myContent_login").focus(); 
    jQuery("#myContent_submit_image").click(function() { 
     jQuery("#login_form_spinner").show(); 
     jQuery.ajax({ 
     type: "POST", 
     url: "Logon.aspx/LoginService", 
     data: "{'username': '" + jQuery("#myContent_login").val() + "', 'password': '" + jQuery("#myContent_password").val() + "','isRemember': '" + jQuery("#myContent_remember_me").is(':checked') + "'}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      if (msg.d != 0) { 
      alert("there isnt error: " + msg.d); 
      } 
     }, 
     error: function (msg) { 
      alert("have error: " + msg.d); 
     } 
     }); 
    }); 
    }); 
</script> 
+0

当你说“有错误:未定义” - 你的意思是“没有错误:未定义”或“有错误:未定义”?这里没有任何代码可以给你“确实存在错误”的确切信息,而且我不确定哪种情况会出错。 – Benj 2011-03-29 20:26:29

回答

0

确保您从您的click处理程序取消默认返回false提交:

jQuery("#myContent_submit_image").click(function() { 

    jQuery('#login_form_spinner').show(); 
    jQuery.ajax({ 
     type: "POST", 
     url: "Logon.aspx/LoginService", 
     data: "{'username': '" + jQuery("#myContent_login").val() + "', 'password': '" + jQuery("#myContent_password").val() + "','isRemember': '" + jQuery("#myContent_remember_me").is(':checked') + "'}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      if (msg.d != 0) { 
       window.location.replace(msg.d); 
      } 
     }, 
     error: function (msg) { 
      alert(msg.d); 
     } 
    }); 

    return false; // THIS IS VERY IMPORTANT 
}); 

而且清理你的AJAX调用,以正确编码参数,就像这样:

jQuery("#myContent_submit_image").click(function() { 
    jQuery('#login_form_spinner').show(); 
    jQuery.ajax({ 
     type: "POST", 
     url: "Logon.aspx/LoginService", 
     data: JSON.stringify({ 
      username: jQuery("#myContent_login").val(), 
      password: jQuery("#myContent_password").val(), 
      isRemember: jQuery("#myContent_remember_me").is(':checked') 
     }), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      if (msg.d != 0) { 
       window.location.replace(msg.d); 
      } 
     }, 
     error: function (msg) { 
      alert(msg.d); 
     } 
    }); 

    return false; // THIS IS VERY IMPORTANT 
}); 
+0

非常感谢@Darin Dimitrov。 Verry专业答案:) – 2011-03-30 18:20:49

1

只是一个快速的观察:

的jQuery( 'login_form_spinner')

你需要一个#或在login_form_spinner前面.

更新:

你可以看看here为例。请注意,成功和错误功能与您的例子不同。

我可以帮助更多,但需要知道具体的错误。

+0

我编辑了你的细节,但它不适用于Firefox和Chrome。但它在ie9中正常工作 – 2011-03-29 20:15:22

0

喜欢justin说你在哪里丢失了或点还你的数据狂变更对象

<script type="text/javascript"> 
jQuery(document).ready(function ($) { 
    $("#myContent_login").focus(); 
    $("#myContent_submit_image").click(function() { 
     $('#login_form_spinner').show(); 
     $.ajax({ 
      type: "POST", 
      url: "Logon.aspx/LoginService", 
      data: { 
       "username":$("#myContent_login").val(), 
       "password":$("#myContent_password").val(), 
       "isRemember":$("#myContent_remember_me").is(':checked') 
      }, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       if (msg.d != 0) { 
       alert("there isnt error: " + msg.d); 
       } 
      }, 
      error: function (msg) { 
       alert("have error: " + msg.d); 
      } 
     }); 
    }); 
}); 
</script> 

,但我怀疑你没有返回JSON头或类似的

+0

你回来的状态是什么,即302 404 – mcgrailm 2011-03-29 20:20:53

+0

没有关于返回的问题。我写到,ie9上的东西很好,但在firefox和chrome中。和我使用jQuery而不是$到.conflict()像http://stackoverflow.com/questions/5456750/i-have-a-jquery-problem-when-using-noconflict – 2011-03-29 20:26:29

+0

如果你这样做,它只是做了它你不必一天都说jQuery,如果你注意到我通过了$ check,那么我可以发送我的项目,如果你愿意的话,我可以发送我的项目http://api.jquery.com/jQuery.noConflict/ – mcgrailm 2011-03-29 20:28:29

0

东西检查jQuery的文档。 ajax细心,你会看到successerror使用不同的参数:

success(data, textStatus, jqXHR) 
error(jqXHR, textStatus, errorThrown) 

所以,你的错误处理程序应该是这样的:

error: function (jqXHR, status, error) { 
    alert("have error: " + status); 
} 

一旦你成功得到错误信息,可以找出导致错误的原因。

+0

没有任何意见了@Benj – mcgrailm 2011-03-29 20:31:17

0

在这一行

jQuery('login_form_spinner').show(); 

它是在后或在原始代码一个错字? 我假设你的选择器缺少一个“#”。由于IE处理这种错误与FF和Chrome不同。你在浏览器中获得不同的行为。

+0

我可以发送我的项目,如果你想。你可以看到我 – 2011-03-29 21:01:31

+0

当然,给我发电子邮件你的项目,我会看看。 [email protected] – 2011-03-29 21:06:00

相关问题