0

Thanks for your help. I am experimenting some trouble when I try to redirect users based on their location through Freegeoip.

May be for you is simple but i can´t get it work, the behaivour should be:

All visitors go to the spanish website www.theWeb.es, but those who browser from out of Spain should be redirected to the international page www.theInternationalWeb.com.

Code:

<script>
     jQuery.ajax( { url: 'https://www.freegeoip.net/json/',
    type: 'POST',  
    dataType: 'jsonp',  
    success: function(location) {
        if (location.country_code === 'ES') {
        // Do nothing because the user is already in the spanish Store.
        else {
        // if the user is not in Spain the send him to the international store.
          window.top.location.href = 'http://theInternationalWeb.com';
         }
    } }}); 
    </script>

Thanks for your time.

Addittional info:

The international website also redirects spanish users to the spanish website, and it seems to be doing it well because it redirects me everytime( i am located in Spain )

<script>


     jQuery.ajax( {
   url: 'https://www.freegeoip.net/json/',
  type: 'POST',
  dataType: 'jsonp',
  success: function(location) {
    if (location.country_code === 'ES') {
      // Redirect him to the Spanish store.
      window.top.location.href = 'http://myWeb.es';} 
  }} );

</script>

IMPORTANT:

I have checked the code and put and alert to detect which location.country_code I have , and it seems that the jsonp is giving me the french country code FR instead of the spanish one ES. So the problem it is not with the code but with the information the FreeGeoIP is giving me. Anyone knows why?

Roberto
  • 155
  • 4
  • 13
  • 1
    `type: 'POST',dataType: 'jsonp',` — The JSONP format demands a GET request. jQuery will ignore your efforts to make this a POST request. – Quentin Sep 26 '16 at 10:17
  • what error are you receiving in console ? – Aatif Bandey Sep 26 '16 at 10:22
  • Have you tried looking at the Console in your browser's developer tools? You have a couple of basic syntax errors that seem to be the cause of your problems. – Quentin Sep 26 '16 at 10:22
  • I will check right away. But the thing that concern me the most is that I have the "same" code in the international website and it redirects me correctly to the spanish website ( It works the same way but redirecting spanish users to the spanish website ). I paste this code to the question too. – Roberto Sep 26 '16 at 10:26
  • is `window.top` in the same origin as `window` ... don't think a sub-window (iframe I hope, not frame!) can redirect the top window if it's cross origin – Jaromanda X Sep 26 '16 at 10:39
  • The redirection works fine, if I only put the code window.top.location.href = 'http://myWeb.es'; or location.href = 'http://myWeb.es' it works. I believe the problem is with the country detection. – Roberto Sep 26 '16 at 10:56

1 Answers1

-1

Use type:'GET'

jQuery.ajax( { url: 'https://www.freegeoip.net/json/', type: 'GET', dataType: 'jsonp', success: function(location) { if (location.country_code === 'ES') { // Do nothing because the user is already in the spanish Store. else { // if the user is not in Spain the send him to the international store. location.href = 'http://theInternationalWeb.com'; } } }});

Aatif Bandey
  • 1,193
  • 11
  • 14
  • The `dataType` is `"jsonp"`, that will force `type: "GET"` automatically. Setting the `type` (to any value) has no effect when a JSONP request is being made. – Quentin Sep 26 '16 at 10:20