Php Slim 3-无法使用ajax post请求中的GET参数重定向到路由
我正在使用ajax post请求将数据发送到使用Slim 3的数据库。所有数据都被正确地发布并插入到我的数据库中,但它不会重定向到GET路径 Ajax Post请求代码Php Slim 3-无法使用ajax post请求中的GET参数重定向到路由,php,jquery,ajax,slim-3,Php,Jquery,Ajax,Slim 3,我正在使用ajax post请求将数据发送到使用Slim 3的数据库。所有数据都被正确地发布并插入到我的数据库中,但它不会重定向到GET路径 Ajax Post请求代码 jQuery(function() { _accent.click(function() { fpd.getProductDataURL(function(dataURL) { var sku = Math.floor((Math.random()
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函数
public function getProductAccent($sku, $hash, Request $request, Response $response) {
在slim 3中,第三个参数是args
-数组,其中包含路径变量,因此实际上需要执行以下操作:
public function getProductAccent(Request $request, Response $response, $args) {
$sku = $args['sku'];
$hash = $args['hash'];
在中,您可以创建相同的方法
public function createProductAccent($sku, Request $request, Response $response) {
到
您是否将路由器插入构造函数中的控制器中?是的,这是在my
BaseController
上,myProductController扩展了它
您确定它不会重定向吗?所以url没有改变?如果我在浏览器中手动插入带有sku
和hash
的url,它将检索数据,但它甚至无法到达该点,因为它首先不会重定向。在上面的代码中,当代码运行到并且不会运行重定向时,我已经给出了注释
public function createProductAccent(Request $request, Response $response, $args) {
$sku = $args['sku'];