我有Ajax请求 这是脚本代码
<script>
$('#variant_model').change(function(){
var value = $('select#variant_model').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type:"POST",
url: "showVariantModel",
data: value,
success: function(data){
alert(data);
}
})
});
</script>
当我输入web.php代码时
Route::post('/showVariantModel', function(){
if(Request::ajax()){
return var_dump(Response::json(Request::all()));
}
});
一切都很好,我在警报中收到了回复。
但是当我想将所有内容放入控制器时,我在控制台中收到错误500
下面,我将从我的web.php和Controller中添加我的代码。 框架是Laravel 5.8
//web.php
Route::post('/showVariantModel', 'VariantsController@checkAttribute');
//VariantsController.php
public function checkAttribute()
{
if(Request::ajax()){
return var_dump(Response::json(Request::all()));
}
谁知道我在做什么错,请给个建议...
更新错误
https://i.stack.imgur.com/iDvvG.jpg
感谢谢克尔·阿里博士(Md.Sukel Ali),我更新了控制器。看起来不是
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Variants;
class VariantsController extends Controller
{
public function checkAttribute(Request $request)
{
if($request->ajax()){
return response()->json($request->all());
}
}
}
现在一切正常。 谢谢。
答案 0 :(得分:2)
缺少结束标记
create TRIGGER ItemTable_OnUpdate
ON ItemTable
INSTEAD OF UPDATE
AS
BEGIN
Declare @s table( IdNew int,IdOld int)
MERGE INTO ItemTable AS dest
USING INSERTED AS ins ON 1=0 -- always false
WHEN NOT MATCHED BY TARGET -- happens for every row, because 1 is never 0
THEN
INSERT (Col, ColOther, Latest)
VALUES (Col, ColOther, Latest)
OUTPUT inserted.ID, ins.ID INTO @s (IdNew, IdOld);
UPDATE ItemTable SET Latest=0 WHERE ID in (select IdOld from @s)
--updating ProductTable
INSERT INTO ProductTable (ItemID, [Value], ValueOther, Latest)
SELECT s.IdNew, pt.[Value], pt.ValueOther, 1 FROM @s s
inner join ProductTable pt on pt.ItemID=s.IdOld
UPDATE ProductTable SET Latest=0 WHERE ItemID in (select IdOld from @s)
END;
答案 1 :(得分:0)
您需要在控制器中添加导入:
use Illuminate/Http/Request;
use Illuminate/Http/Response;
答案 2 :(得分:0)
不要盲目猜测ajax调用背后发生的情况。我建议您学习如何调试Ajax请求。
转到浏览器,右键单击并检查,然后转到“网络”选项卡,您可以看到您的请求。单击您的请求,然后查找响应选项卡。在那里您可以找到发生的确切情况。
public function checkAttribute(Request $request)
{
if($request->ajax())
{
return response()->json($request->all());
}
}