2014-04-04 40 views
0

此代碼在XP機器上除IE8以外的其他所有瀏覽器都可以工作。不能爲我的生活弄清楚。在IE8中,它將始終顯示錯誤功能。我試圖將dataType更改爲jsonp,text,html,並且不管它總是彈出錯誤函數。就像我說的,Chrome,Safari,Firefox和其他所有IE的工作,只是不是IE8。IE8 Ajax提交總是拋出錯誤功能

<script type="text/javascript"> 
    $("#zip").submit(function (e) { 
     e.preventDefault(); 
     if ($(this).parsley('isValid') === true) { 
      var el = $("#zipcode"); 
      if ((el.val().length == 5) && (is_int(el.val()))) { 
       $.ajax({ 
        url: "http://zip.elevenbasetwo.com/v2/US/" + el.val(), 
        cache: false, 
        dataType: "json", 
        type: "GET", 

        success: function (result, success) { 
         console.log(result.state); 
         $('.rates-zip').fadeOut(function() { 
          switch (result.state) { 
          case "California": 
           $('#western').fadeIn(); 
           break; 
          case "Illinois": 
          case "Virginia": 
           $('#midwest').fadeIn(); 
           break; 
          case "New York": 
          case "New Jersey": 
           $('#eastern').fadeIn(); 
           break; 
          case "Washington": 
           $('#northwest').fadeIn(); 
           break; 
          default: 
           $('#default').fadeIn(); 
          } 
         }) 



        }, 
        error: function (result, success) { 
         alert("Error IE8"); 
        } 
       }); 
      } 
     }; 

    }); 

    function is_int(value) { 
     if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
</script> 

希望有人可能有解決方案。

+0

檢查操作錯誤。它說什麼? –

+1

是zip.elevenbasetwo.com你的域名和你使用的是什麼版本的jQuery?我不認爲IE8支持CORS的方式與其他更現代的瀏覽器相同。 –

+0

您可能會遇到IE8與e.preventDefault()不兼容的情況之一,請參閱:http://stackoverflow.com/questions/21033728/jquery-preventdefault-and-ie8-clarification – Culyx

回答

0

檢查是否已在jQuery的啓用CORS:

$.support.cors = true; 

此外,請確保您最遲的jQuery 1.9.1在IE8上運行。 jQuery 2.x支持IE8。

您可能需要根據IE版本動態地將正確版本的jQuery插入到您的DOM中。

像這樣的東西可以工作:

var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); 
var ie = re.exec(navigator.userAgent)?parseFloat(RegExp.$1):10; 

if(ie >= 10) 
    document.write("<script src='/js/jquery2.1.0.js'><\/script>"); 
else 
    document.write("<script src='/js/jquery1.9.1.js'><\/script>"); 

而且,與IE8,你需要使用一個plugin,以實現與XDomainRequest CORS因爲它不支持XMLHttpRequest。由於IE8是一個快速老化的瀏覽器,jQuery團隊不打算將此支持添加到jQuery核心。

除此之外,您的遠程服務器也需要支持CORS。它需要通過Access-Control-Allow-Origin響應頭允許所有的來源。如果這個頭部不存在於響應中,它將不會返回200 OK響應。