2012-01-27 62 views
2

我有一個相當簡單的基於jQuery的ajax調用。它在IE9,Firefox最新和Chrome最新(所以我非常確定AJAX調用張貼到的頁面很好),但在IE8(未嘗試IE7),它失敗了。IE8中的jQuery ajax調用錯誤

jQuery的代碼是:

$('.to-step-2').click(function(){ 
    var d = new Date(); 
    var roomShape; 
    blnError = false; 
    $.ajax({ 
    url: '/base/RoomBuilder/GetRoomShape.aspx?_='+d.getTime(), 
    async: false, 
    type: 'post', 
    cache: false, 
    dataType: 'html', 
    success: function(data){ 
     if(data.substring(0,5) == 'Error'){ 
     alert('Please select a room shape to continue'); 
     blnError = true; 
     }else{ 
     roomShape = data; 
     } 
    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
     alert('Error 6: jqXHR = ' + jqXHR + '\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown); 
     blnError = true; 
    } 
    });  
    if (blnError == true){ 
    return false; 
    } 

的錯誤,這是在IE8僅拋出是閱讀:

Error 6: jqXHR = [object Object] 
textStatus = error 
errorThrown = Length Required 

我已經看到了關於類似的事情了幾個其他職位,但添加時間戳和緩存:假,以防止緩存似乎是相當常見的解決方案,但仍然不適合我:(

任何人都可以看到爲什麼發生這種情況,並建議修復?

+0

看看http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/ – Stefan 2012-01-27 11:33:59

回答

3

你從你的服務得到一個411 response

411 Length Required

The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

這是因爲,在發帖時,如果沒有數據的jQuery不設置Content-Length頭。這是一個安全的要求張貼到IIS時:

The underlying issue is that most installations of IIS6+ require a content-length be provided with all POST requests, even if there is no content (POST data).

The content-length for a request with no data should be 0, but jQuery doesn’t set that header automatically unless there is a data parameter. Since ASP.NET AJAX’s JSON serialized services require a POST request, this becomes a stumbling block for read-only requests.

(Quoted from from http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/)

數據參數只要設置爲空的對象似乎是最簡單的解決方法:

$.ajax({ 
url: '/base/RoomBuilder/GetRoomShape.aspx?_='+d.getTime(), 
async: false, 
type: 'post', 
cache: false, 
data: "{}", 
dataType: 'html', 
success: function(data){ 
    if(data.substring(0,5) == 'Error'){ 
    alert('Please select a room shape to continue'); 
    blnError = true; 
    }else{ 
    roomShape = data; 
    } 
} 
3

按錯誤:errorThrown =長度必需,嘗試加入僞數據,如:

 

$.ajax({ 
    url: '/base/RoomBuilder/GetRoomShape.aspx?_='+d.getTime(), 
    async: false, 
    type: 'post', 
    cache: false, 
    dataType: 'html', 
    data: {}, 
    ..... 
 

希望工程

+1

'data:「{}」'可能已經足夠 – Stefan 2012-01-27 11:33:18

+0

是真的,改變了因此 – 2012-01-27 11:34:14

+0

感謝Sudhir - 雖然需要圍繞花括號的引號,但是它的確有用。 – Dan 2012-01-27 11:38:02