1

我使用jquery-1.7.1.min.js。JQuery - 簡單的Ajax請求在IE中不起作用

簡單的例子:在Opera,Chrome瀏覽器,火狐,Safari

<script language="JavaScript"> 
$(document).ready(function(){ 

    $.ajax({ 
     type: "POST", 
     url: "/ajax/test.php", 
     data: "name=John&location=Boston" 
    }).done(function(msg) { 
     alert("Data Saved: " + msg); 
    }); 

}); 

</script> 

顯示警告信息,但不顯示在IE7-IE9警報消息。問題在哪裏?如何解決它?

回答

0

嘗試將其更改爲此以使用「成功」參數而不是「完成」方法。我還將「語言」屬性更改爲「類型」

<script type="text/javascript"> 

$(document).ready(function(){ 
    $.ajax({ 
     type: "POST", 
     url: "/ajax/test.php", 
     data: "name=John&location=Boston", 
     success: function(msg) { 
      alert("Data Saved: " + msg); 
     } 
    }); 
}); 

</script> 
+0

只是試圖解決,但不反正工作:( – WebDeveloper 2012-02-20 10:41:29

+0

有沒有在IE中顯示錯誤信息? – fin1te 2012-02-20 10:45:24

+0

這似乎沒什麼問題,應該在工作任何瀏覽器 – 2012-02-20 10:49:51

0

嘗試包括dataType以及您提供的其他選項。

<script type="text/javascript"> 

$(document).ready(function(){ 
    $.ajax({ 
     type: "POST", 
     url: "/ajax/test.php", 
     data: "name=John&location=Boston", 
     dataType: 'json', // <-- OR HTML,XML etc 
     success: function(msg) { 
      alert("Data Saved: " + msg); 
     } 
    }); 
}); 

</script> 
0

另一種方式來做到這一點

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $.ajax({ 
    type: "POST", 
    url: "/ajax/test.php", 
    data: "name=John&location=Boston", 
    dataType: "text", 
    complete: function(msg,status) {alert("Data Saved: " + msg.responseText);} 
    }); 
}); 
</script> 

另一種解決方案,請嘗試以下

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     $.post('/ajax/test.php',{name:'John',location:'Boston'}, 
     function(data) {alert("Data Saved: " + data);} 
    ); 
    }); 
</script> 

並添加,在test.php的標題:

<?php 
header("Cache-Control: no-cache"); 
header("Pragma: no-cache"); 
echo "It works !" // Be positive :-) 
?> 
+0

只有這個代碼見ie9。我在test.php中只寫了一行代碼:echo「test」;在IE 9中ajax返回警告消息「Data保存的內容:「undefined」!爲什麼?!在另一個瀏覽器中,它的效果很好 – WebDeveloper 2012-02-20 13:42:54

+0

我可以建議您將整個網址你的http://...domain.../test.php。看看我的第二個解決方案。並告訴我它是否有效。 – Valky 2012-02-20 17:08:34

2

當初一樣的問題。通過添加頭PHP的一部分(test.php的)

header("Content-type: text/html; charset=windows-1251");