2011-03-29 18 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

相關問題