Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving cookie from AJAX http redirect #1

Closed
ghost opened this issue Jan 6, 2017 · 4 comments
Closed

Retrieving cookie from AJAX http redirect #1

ghost opened this issue Jan 6, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Jan 6, 2017

Retrieving a cookie upon successful AJAX entry submission with http redirect (a cookie is set on redirect page - submitted entry) won't update correct value except manual page refresh.

On the redirected page I'm checking a value variable against the retrieving cookie which gives me the expected result but the cookie doesn't give me expected result when I retrieve it somewhere else in other templates. Except page refresh.

Why?

@khalwat
Copy link
Collaborator

khalwat commented Jan 6, 2017

I don't know. Can you post exactly how you're setting and retrieving the cookie (the actual code)?

@ghost
Copy link
Author

ghost commented Jan 6, 2017

There is a guest entry submission with AJAX POST and redirect at the end.
The code is stripped-down.

AJAX post

<script type="text/javascript">
$('#add-to-cart').submit(function(ev) {
// Prevent the form from actually submitting
ev.preventDefault();

// Get the post data
var data = $(this).serialize();

// Add the CSRF Token
data[csrfTokenName] = csrfTokenValue;

// Send it to the server
$.post('/', data, function(response, textStatus, jqXHR) {

    if (response.success) {

    // Set countdown and redirection
    var count=2;
    var counter=setInterval(timer, 1000); //1000 will run it every 1 second

	function timer()
	{
	 count=count-1;
	 if (count <= 0)
	   {
              clearInterval(counter);

    //counter ended
    // HTTP redirect
    window.location.replace("{{ siteUrl }}uriA/uriB/{{ token }}");
	   }
	}

	 } else {
	   alert('Hmm..?');
	   }
     });

});
</script>

Redirected page
This page is only reached upon successful entry submission per user's session.
So here I set a simple secure cookie variable.

 {# Set cookie to store cart total. #}
 {% do craft.cookies.setSecure('cartTotal', total) %}

Than
In other template I simply check it if it has any length set like this.

{% if craft.cookies.getSecure('cartTotal')|length %}
    {{ craft.cookies.getSecure('cartTotal') }}
{% else %}
    0
{% endif %}

So when an entry is submitted and I get to redirect page destination the cookie cartTotal is already set in the browser but not retrieving it only upon manual page refresh.
That's the whole story :)

@ghost
Copy link
Author

ghost commented Jan 21, 2017

Such rookie mistake. I had cookies variables defined after {% extends "" %} tag which inherited undefined cookie variable before It got set on first page redirect.

@ghost ghost closed this as completed Jan 21, 2017
@khalwat
Copy link
Collaborator

khalwat commented Jan 21, 2017

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant