jQuery AJAX POST适用于除IE以外的所有浏览器

时间:2012-07-26 14:30:02

标签: jquery ajax html5 internet-explorer post

我见过一些像我这样的问题,但大多数修复似乎并不相关。我有以下jquery采用提交的表单,encypts传递SHA256,将表单序列化为JSON对象,然后使用json stringify将json格式化为明文格式以传递给ASPX服务。该服务返回XML。这适用于Firefox,Chrome,Safari,Opera,iOS Safari,Blackberry和默认的Android浏览器。在IE上,它总是调用错误。有人有任何想法吗?

$(function() {
  $('form').submit(function() {
   $('#password').val($.sha256($('#password')));
    var jsonTest =  JSON.stringify($('form').serializeObject());
    $('#result').text(jsonTest);
    $.ajaxSetup({cache: "false"});
    $.ajax({
        type: "POST",
        url: "http://mydomain.com/JSONService.asmx/WebRequest",
        data: "request=" + jsonTest,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        crossDomain: true,
        dataType: "xml",
        success: function(xml){

                var myVals = $(xml).find('string').text();
                $('#result').text(myVals);
                alert(myVals);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Error: " + errorThrown);
        }
    });
return false; 

我的(相关)HTML:

<!doctype html>
<!-- Conditional comment for mobile ie7 blogs.msdn.com/b/iemobile/ -->
<!--[if IEMobile 7 ]>    <html class="no-js iem7" lang="en"> <![endif]-->
<!--[if (gt IEMobile 7)|!(IEMobile)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

<head>
  <meta charset="utf-8">

  <title>Test Form</title>
  <meta name="description" content="">

  <!-- Mobile viewport optimization h5bp.com/ad -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width">

  <!-- Mobile IE allows us to activate ClearType technology for smoothing fonts for easy reading -->
  <meta http-equiv="cleartype" content="on">

  <!-- more tags for your 'head' to consider h5bp.com/d/head-Tips -->

  <!-- Main Stylesheet -->
  <link rel="stylesheet" href="css/style.css">

  <!-- All JavaScript at the bottom, except for Modernizr which enables HTML5 elements & feature detects -->
  <script src="js/libs/modernizr-2.0.6.min.js"></script>
</head>

<body>

  <div id="container">
    <div id="main" role="main">
        <div id="logBox">
        <img alt="Logo" src="img/logo.png" style="margin-left: 5px;" />
        <div id="blackHead">Please Sign-In To Continue</div>
        <form name="login" method="post" action="#">
<label for="sessionemail">Email</label><br />
<input autofocus="autofocus" autocapitalize="off" maxlength="150" id="sessionemail" name="sessionemail" title="Your email" type="text" value="" class="inputText" /><br />
<br />
<label for="password">Password</label><br />
<input type="password" name="password" id="password" title="Password" value="" class="inputText" /><br />
<br />
<input type="hidden" name="sessionid" id="sessionid" value="<?php echo $sessionNumber ?>"  />
<input type="hidden" name="subtocall" id="subtocall" value="g2.web.login.sub" />
<input type="submit" value="Sign-In" name="submit" class="submitBox" />

</form>
    <div id="greyFoot">Forgot your password? <a href="#">Click here now!</a><br />
        <pre id="result">
</pre>
    </div><!-- end #greyFoot -->
    </div><!-- end #logBox -->
    </div><!--end #main -->
  </div> <!--! end of #container -->
</body>
</html>

我非常感谢任何帮助。 Firebug并没有抛出任何错误,我真的很难知道除了IE之外它是如何工作的。我习惯于CSS而不是JS!

编辑 - 我更改了错误以显示errorThrown,其中显示“No Transport”。

1 个答案:

答案 0 :(得分:1)

我不确定这是否是本地域请求,但是它是否是远程请求,即不支持跨域ajax调用,无论其是否为getJSON。学到了很难......