I am using ajax post request to send data to my database using Slim 3. All the data gets posted and inserts into my database correctly but it will not redirect to the GET path.
Ajax Post request code
jQuery(function() {
_accent.click(function() {
fpd.getProductDataURL(function(dataURL) {
var sku = Math.floor((Math.random() * 10000000000) + 1);
$.ajax({
type: "POST",
url: "{{ path_for('product.createProductAccent', {sku: product.sku}) }}",
data: {
sku: sku,
img: dataURL
}
});
});
});
});
These are my Routes
$app->post('/golf-bags/accent/{sku}', ['Base\Controllers\ProductController', 'createProductAccent'])->setName('product.createProductAccent');
$app->get('/golf-bags/accent/{sku}/{hash}', ['Base\Controllers\ProductController', 'getProductAccent'])->setName('product.getProductAccent');
This is my ProductController POST and GET functions
public function createProductAccent($sku, Request $request, Response $response) {
$product = Product::where('sku', $sku)->first();
$hash = bin2hex(random_bytes(32));
$uploads = Upload::where('sku', $sku)->first();
$path = __DIR__ . '/../../public/assets/uploads/';
$img = $request->getParam('img');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = mt_rand() . '.png';
file_put_contents($path . $file, $data);
$sku = $request->getParam('sku');
$uploads = Upload::create([
'sku' => $sku,
'hash' => $hash,
'accent_colour' => $file
]);
/****
ALL THE CODE RUNS UPTO HERE AND INSERTS INTO DB
BUT WILL NOT REDIRECT WITH THE RESPONSE BELOW
****/
return $response->withRedirect($this->router->pathFor('product.getProductAccent', [
'sku' => $sku,
'hash' => $hash
]));
}
public function getProductAccent($sku, $hash, Request $request, Response $response) {
$product = Product::where('sku', $sku)->first();
$design = Design::where('sku', $sku)->first();
$uploads = Upload::where('hash', $hash)->first();
$colours = Colour::all();
$array = [];
foreach($colours as $colour) {
$array[] = $colour->hex_colour_bg;
}
return $this->view->render($response, 'product/product-design-accent.php', [
'product' => $product,
'design' => $design,
'uploads' => $uploads,
'colours' => json_encode($array)
]);
}
Not sure what i have wrong here, but it just will not redirect to the GET function.