1

Requirement is i need to pass the visitors country code by default as part of HTTPRequest. I have the following jquery code in my common.js

$(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'));
        }       
    }   
});

Now how do i pass this country code with every request by default to webserver. I do NOT want to pass it as query parameters, because I have to end of modifying all my URLs which is a pain. Any ideas? Can I somehow pass it as part of HTTPRequest header?

Abdus Samad
  • 343
  • 2
  • 12
  • 27

1 Answers1

0

Answered in this question

$.ajaxSetup({
    beforeSend: function (xhr)
    {
       var countryCode = window.sessionStorage.getItem('country');
       xhr.setRequestHeader("CountryCode", countryCode);        
    }
});
Community
  • 1
  • 1
aTable
  • 58
  • 2
  • 7