Reputation: 3078
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
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