views:

55

answers:

1

I have a problem with base tag. It looks like
<base href="http://myexamplepage.com/myfolder/" />.

Everything works, besides this query:
$.get("application/soft/calendar_month_change.php", ...)

My computer thinks it's cross domain server and changes query to OPTIONS .... When I remove the base tag, it works correctly but my site doesn't show any images. I use smarty template engine. How can I solve it?

+2  A: 

How can I solve it?

Want my opinion? Don't use base. Exactly for the reason presented here: It creates confusion, and influences other parts of the system in a way that is extremely difficult to debug. (Although I didn't know it even influences relative AJAX calls.)

I would define a web root in Smarty, and prepend that for every image URL in the document. Changing the web root remains easy, but the browser is given absolute URLs.

You could, however, also solve this by providing an absolute URL in the Ajax request and leave the base in place.

$.get("http://domain.com/application/soft/calendar_month_change.php", ...)
Pekka
I tried ($.get("http://domain.com/application/soft/calendar_month_change.php", ...)) that too but I thought it didn't work as well. I checked it now and query is correct (GET ....). What is strange, I don't get any content(when I did it without a base tag it worked).`$.get("http://mypage.pl/myfolder/application/soft/calendar.php", {day: selectedDay, month: month, year: year}, function(data){ alert(data); <--- it doesn't show anything });'
Paweł
@Pawel just for clarity. Both the HTML page and `calendar.php` are running on `mypage.pl`? Both on the same protocol `http://` ?
Pekka
that's right. I added www. and it works. I see the problem, but how can I easily change it not to have that base tag. Adding everywhere {$url} wouldn't be funny :P. What about css files. I load images there for background.
Paweł
@Pawel you added "www" where? An absolute URL in the Ajax call should work. It just needs to have the *exact same domain* as the HTML page because of the Single Origin Policy. You could then leave your base tags in place.
Pekka
There is another problem. When I type www.mypage.pl/myfolder/ it works correctly. When I type mypage.pl/myfolder it doesn't work. It depends on what I write in $.get(...). So when I have $.get("http://www.mypage.pl/myfolder"...) I have to type in the browser www.mypage.pl/myfolder. When I write it without www. it won't work. It's not what I want
Paweł
@Pawel I see. But that's not `base` 's fault: It's the single origin policy for Ajax calls. You would have to dynamically add the host name: `$.get("http://{$hostname}/myfolder.....");` You should be able to get the host name from PHP`s `$_SERVER["HTTP_HOST"]`
Pekka
I see. Thanks for help
Paweł