Today I stumbled over some weird issue with jQuery and webkit browsers when sending a POST request. While in Firefox I get the response (with a status code 200) in webkit browsers (chrome, midori, safari) a 204 (no content) status code is returned. Switching to GET solves this.
I’ve used the following code:
$('#button').on('click', function () { $.ajax({ type: "POST", dataType: 'text', url: "/" + language + "/test.html", data: { 'shopAdd': "true", 'product': "5" } }) .done(function (msg) { alert("Data Saved: " + msg); }) .fail(function () { alert("error"); }); return false; });
In the called page is a snippet which does:
$_SESSION['Shop'][] = $_POST['product']; echo time(); die();
So there is no output apart from a timestamp. I am using Contao here, so the 204 might be returned by Contao instead of by the Server but then: In firefox the correct response is returned. Modifying the above to use GET instead of POST makes it work in firefox as well as webkit browsers.
I don’t even have any idea how to debug that.
In a free moment Sven (thanks) took a look and found the following snippet within Contao:
if ($_POST && !RequestToken::validate(Input::post('REQUEST_TOKEN'))) { // Force a JavaScript redirect upon Ajax requests (IE requires absolute link) if (Environment::get('isAjaxRequest')) { header('HTTP/1.1 204 No Content'); header('X-Ajax-Location: ' . Environment::get('base') . 'contao/'); }
So basically: Contao sends a 204 no content, if it is an ajax (POST) request and if the Request token has not been submitted. Still I have no idea WHY this does work without a request token in firefox.
It does work in Firefox as long as you’re logged in to the backend. If you logout it won’t work in Firefox neither. Who wants a COOKIE!
No Comments