我在产品控制器的jQuery ajax帖子上收到400(错误请求)。我正在尝试post
array
到我的数据库。
路径attr
<a id="store-product" data-path="{{ path_for('product.design', {sku: design.sku}) }}">Submit</a>
Ajax代码
/** Call to the Fancy Product Designer **/
fpd = new FancyProductDesigner(_container, pluginOpts);
var _storeProduct = jQuery('#store-product');
_storeProduct.click(function() {
var url = _storeProduct.attr("data-path");
var productViews = fpd.getProduct();
/*******
//console.log(productViews);
//Works Fine upto here with the log array below
//Array[5]0: Object1: Object2: Object3: Object4: Objectlength: 5__proto__: Array[0]
*******/
jQuery.ajax({
url: url,
type: "post",
data: JSON.stringify({
action: 'store',
views: productViews
}),
contentType: "application/json; charset=utf-8",
success: function (data) {
if(parseInt(data) > 0) {
alert('Product with ID ' + data + ' stored!');
} else {
alert('Error: ' + data + '');
}
}
});
});
不确定我的错误。
控制器代码
public function design($sku, Request $request, Response $response) {
$design = Design::where('sku', $sku)->first();
if($request->getParam('action') == 'store') {
$views = $request->getParam('views');
$design->update([
'views' => $views
]);
return $response->withRedirect($this->router->pathFor('product.get', [
'sku' => $sku,
]));
}
}
更新
Apache Access Log
::1 - - [18/Jan/2017:16:27:25 +0700] "POST /projects/GolfBag/public/golf-bags/design/2563901 HTTP/1.1" 400 18 "http://localhost/projects/GolfBag/public/golf-bags/2563901" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
路线
$app->get('/golf-bags/{sku}', ['Base\Controllers\ProductController', 'getProducts'])->setName('product.get');
$app->post('/golf-bags/design/{sku}', ['Base\Controllers\ProductController', 'design'])->setName('product.design');
答案 0 :(得分:1)
经过一番挖掘后,我发现问题是CSRF令牌未应用于帖子数据