How to pass Accept header in jQuery .load() function
How to pass Accept header in jQuery .load()开发者_开发问答 function as It need to be passed.
You need to utilize the beforeSend
parameter in the ajax
method, since load
does not expose this functionality:
$.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);
}
});
As mentioned in an earlier answer by Jon, load
can't do it by itself. I prefer to use the headers
option on the ajax
method rather than 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.
}
});
The end result is the same but if I like saving keystrokes.
精彩评论