Hello,
I have a problem with all AJAX -requests getting 301 redirect. AFAIK this leads to the POST -data sent with the request getting cleared.
Below I is an example about what I'm trying to do.
My question is:
Is there something I don't understand about Processwire or internet technology which is causing this behaviour?I have page with a button. When you click this button, an HTML overlay popup (done with Foundation and Reveal) with option to enter email is displayed (The content of this popup is a processwire template which is loaded with AJAX -request. Causes also 301 redirect, but this doesn't use POST -data, so I didn't realise the problem at this point)
Then this data is sent as POST -request with payload to processwire for processing The processing is done by special API -template, which exists solely for this purpose of processing data. However the payload in the POST -request never reaches the API -template I believe this is because a 301 redirect happens before the the POST -request reaches the API -template
Thanks for reading and special thanks for any answers!
Example code:
Popup (HTML & JavaScript):
<!-- use onsubmit to override the standard 'sending form to self' -behaviour
<form onsubmit="WEBSITE.dialog.download.mail($('#email').val(), event); ">
<label>email:</label>
<input id='email' name='email' type='email' value=''>
<input type="submit" value="Send">
</from>
The function which sends the POST -request to processwire template (which processes API requests) (the function is in separate .js file)
// I've translated this from CoffeeScript to JS without testing, hope there's no errors
dialog = {
download: {
mail: function (email, ev){
// Prevent default behaviour of submit -functionality
ev.preventDefault();
// Success and error callbacks
success = function(response){
console.log('success', response);
}
error = function(response){
console.log('error', response);
}
// Do ajax -request with jQuery
request = $.ajax({
url: '/api/mailer/send/',
method: "POST",
data: {email: email},
success: success,
error: error
});
}
}
}
I've also tried this with doing the request without jQuery, but result is the same.
The API template (PHP):
?php
// Redirect non-ajax calls
// Get the API address from url
$api_url = explode('/api/', $page->url);
if(!isset($api_url[1])){
// This url is not in correct format, send error
die(createJSONResponseError(
1,
'Invalid url format.'
)
);
}
switch ($api_url[1]):
case 'mailer/send/':
// This is where it stops every time, though I've checked from Chrome dev console that the email key/value pair exists (at least when request is made)
if($input->post->email === null){
die(createJSONResponseError(
2,
'Insufficient params.'
)
);
}else{
// Create the response
$response = createJSONResponse(
'success',
null
);
}
break;
// Default action - needs to be last of the list
default:
// So this api does not exist, send error
die(createJSONResponseError(
1,
'Unknown API.'
)
);
break;
endswitch;
// Send the response
die($response);
?>
Here's how the responses are created (from a helper methods file):
/**
* Create JSON response for httpXMLRequest
*
* @param status - a string that represents the status of this operation. Recommended values: 'success' or 'error'
* @param result - the data resulting this request.
*
* @return JSON object
*
*/
function createJSONResponse($status, $result){
return json_encode(
array(
'status' => $status,
'result' => $result
)
);
}
/**
* Create JSON error response for httpXMLRequest
*
* @param code - error code
* @param msg - error message
*
* @return JSON object
*
*/
function createJSONResponseError($code, $msg){
return createJSONResponse(
'error',
array(
'code' => $code,
'msg' => $msg
)
);
}