Hardik Thaker
Hardik Thaker

Reputation: 3078

XMLHttpRequest cannot load when URL is with www

I have implemented autosuggest script using jQuery on my website. When browser load my website with www ( i.e. www.xyz.com ) search suggestion doesn't works and prints the following error in browser console :

XMLHttpRequest cannot load http://xyz.com/json.php?json=true&limit=15&input=testing. Origin http://www.xyz.com is not allowed by Access-Control-Allow-Origin.

I know that jQuery / Chrome doesn't allows me to make cross domain AJAX call but when I access my website without www ( i.e. just xyz.com) everything works perfectly. How can I solve this problem ? Does any one has Idea about it ?

Upvotes: 1

Views: 310

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You are violating the same origin policy restriction.

http://www.xyz.com and http://xyz.com are considered cross domains according to this policy.

To solve the problem just use a relative url in your $.ajax() request:

$.ajax({
    url: '/json.php',
    ...
});

instead of absolute:

$.ajax({
    url: 'http://xyz.com/json.php',
    ...
});

Upvotes: 4

Related Questions