我正在使用 ajax 发布请求使用 Slim 3 将数据发送到我的数据库。所有数据都被发布并正确插入到我的数据库中,但它不会重定向到 GET 路径。
Ajax Post 请求代码
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
}
});
});
});
});
这些是我的路线
$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');
这是我的 ProductController POST 和 GET 函数
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)
]);
}
不知道我在这里有什么问题,但它不会重定向到 GET 函数。