2011-09-23 26 views

回答

0

如在earlier answer by Jon提到,load本身不能做到這一點。我更喜歡使用headers選項上ajax方法,而不是beforeSend

$.ajax({ 
    url: "http//example.com", 
    headers: { 
     Accept: "application/whatever" // Use the actual type you need. 
    }, 
    success: function (data) { 
     // Put the data into the element you care about. 
     $("#foo").html(data); 
    }, 
    error: function (jqXHR) { 
     // Put whatever you need to do if the query fails here. 
    } 
}); 

最終的結果是一樣的,但如果我喜歡節約的按鍵。

+0

好的,如果我想用head,body等替換所有的html,我應該使用哪個選擇器? – rcanovas

+1

@rcanovas我不知道是否有適當的選擇器爲jQuery來做到這一點,但你可以使用普通的JS:'document.documentElement.innerHTML = data' – Louis

6

你需要利用在ajax方法beforeSend參數,因爲load不公開此功能:

$.ajax({ 
    url: "http://www.server.com/", 
    beforeSend: function(jqXHR, settings) { 
     jqXHR.setRequestHeader("Accept", "application/json"); 
    }, 
    // You need to manually do the equivalent of "load" here 
    success: function(result) { 
     $("selector").html(result); 
    } 
});