Answer the question
In order to leave comments, you need to log in
Why does javascript not work if "www." is added to the domain, and how to get out of the situation?
Dear friends, on my website (on opencart ) there is a jquery script that performs an ajax request (the "Order" button) .
So, it works on a domain without "www" and does not work stably with "www" .
What to do?
Answer the question
In order to leave comments, you need to log in
If you look at the contents of a page that opens with www, then the address without www will be written in the "BASE" tag. This is such a feature of opencart. In the "order" button, the address looks like "index.php?route=bla-bla-bla". Since this address does not have a "/" character at the beginning, the browser will take the value from BASE to build the full address. Then it turns out that on the page " http://www.site.ru" the link for the "order" button looks like " http://site.ru/index.php?route=bla-bla-bla". And this is already a cross-domain request, which in your case does not work.
As a solution, all pages should open on the same domain. You need to make a redirect from www-addresses to addresses without www using .htaccess.
RewriteCond %{HTTP_HOST} ^www.site.ru
RewriteRule (.*) http://site.ru/$1 [R=301,L]
Because domain.com and www.domain.com from the browser's point of view are different domains, and cross-domain ajax is prohibited in browsers
In the script (where AJAX) do not write the full address like:
but write:
browser will substitute either www.domain.com or domain.com depending on the context
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question