开发者

How to pass Accept header in jQuery .load() function

开发者 https://www.devze.com 2023-04-07 02:44 出处:网络
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 functionali

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消