php - Cookies not visible after ajax/load request

304

I have the following problem:

On one page I display some articles using php and grabbing them from the database. The articles have two display types: daily and hourly. The display type is stored in a cookie.

The problem is that whenever i load additional articles using infinite scroll and ajax the cookies are not taken. If i echo them I receive an empty string. Also when changing from the hourly view to the daily view the same problem occours.

For adding additional post i use jquery post and for loading the diferent display view i use jquery.load()

I know that there was a cross origin policy but i fixed it by adding in htaccess

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

EDIT: I installed the website also on another server where it works ok. I noticed that on the good server tha ajax request is sending also the "settings" cookie. On the bad server only the "PHPSESSID" cookie is beeing sent.

342

Answer

Solution:

ok it seems the problem was the www the request was made from www.mydomain.com to mydomain.com i fixed it by adding the full path in the url

$.ajax({
   url: "www.mydomain.com/ajax.php",
   success: function() ...
});

or

 SITE_URL = "http://www.mydomain.com";
 $('#wrapper').load(SITE_URL 'index.php?lang='+langSite+' #wrapper', function(data){ ...

People are also looking for solutions to the problem: php - Change background image on menu hover in Wordpress

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.