Javascript 作为http头的一部分传递国家代码

Javascript 作为http头的一部分传递国家代码,javascript,jquery,html,http,webserver,Javascript,Jquery,Html,Http,Webserver,要求是我需要在默认情况下通过访客国家代码作为HTTPRequest的一部分。 我在common.js中有以下jquery代码 $(function() { if (window.sessionStorage) { if($('#country').val() == '') { if(window.sessionStorage.getItem('country') == null) {

要求是我需要在默认情况下通过访客国家代码作为HTTPRequest的一部分。 我在common.js中有以下jquery代码

$(function() {

    if (window.sessionStorage) {
        if($('#country').val() == '')
        {
            if(window.sessionStorage.getItem('country') == null)
            {
                if(window.localStorage.getItem('country') == null)
                {
                    $.get("http://ipinfo.io", function(response) {
                        window.sessionStorage.setItem('country', response.country);
                        window.localStorage.setItem('country', response.country);
                    }, "jsonp");
                }
                else
                {
                    window.sessionStorage.setItem('country', window.localStorage.getItem('country'));
                }
            }
            $('#country').val(window.sessionStorage.getItem('country'));
        }       
    }   
});
现在,在默认情况下,如何将每个请求的国家代码传递给webserver。我不想将其作为查询参数传递,因为我必须结束对所有URL的修改,这很痛苦。有什么想法吗?我可以把它作为HTTPRequest标题的一部分传递吗?

回答如下

$.ajaxSetup({
发送前:函数(xhr)
{
var countryCode=window.sessionStorage.getItem('country');
setRequestHeader(“CountryCode”,CountryCode);
}
});在本节中回答

$.ajaxSetup({
发送前:函数(xhr)
{
var countryCode=window.sessionStorage.getItem('country');
setRequestHeader(“CountryCode”,CountryCode);
}

});cookie似乎很简单所有http请求上都有足够的cookie发送到服务器…不难实现也不难研究cookie似乎很简单所有http请求上都有足够的cookie发送到服务器…不难实现也不难研究