jimit
jimit

Reputation:

URL Problem in Ajax in PHP

When I open my site without "www", like http://mysite.com/, then there is a problem with my website hit counter on the home page, which is done through AJAX.

The problem is that counter Image is not getting displayed. It is showing blank. There are similar problem on other pages where I have used AJAX to retrieve data.

Upvotes: 0

Views: 210

Answers (2)

mike
mike

Reputation: 5213

You can create .htaccess file in your root dir and put this text inside

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC] 
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

This will make sure that every time user enters http://mysite.com, it gets redirected to http://www.mysite.com

The server has to support .htaccess and mod_rewrite

Upvotes: 2

Greg
Greg

Reputation: 321588

To the cross-domain security policy, "mysite.com" and "www.mysite.com" are different domains, therefore AJAX requests aren't allowed between them.

The simplest solution is to take the domain out of your AJAX call and use a relative url, for example "/dir/ajax-callback.php" instead of "http://www.mysite.com/dir/ajax-callback.php"

Upvotes: 6

Related Questions