R
R
Run Utochkin2014-03-31 12:17:51
JavaScript
Run Utochkin, 2014-03-31 12:17:51

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

4 answer(s)
N
nowm, 2014-03-31
@Run

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]

This will be an easier solution than fixing the addresses in the Opencart engine, since I found about 90 addresses there, which in this case will have to be corrected. For example, by editing the address you will correct the work of the “order” button, but the buttons “add to wishlist”, “add to comparison”, etc. will not work from this. There are a lot of those moments. So, setting up a redirect in .htaccess will solve all these problems at once.

R
Ruslan Kasymov, 2014-03-31
@HDAPache

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

R
RubaXa, 2014-03-31
@RubaXa

CORS

D
Denis, 2014-03-31
@Nedbow

With and without WWW are 2 different domains, use jsonp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question