[英]In Laravel, how to simply pass data from js to controller back to view?
I basically need to click a button in my blade view that triggers a function in my js file, that then sends some values to a controller and back to the the view.我基本上需要在我的刀片视图中单击一个按钮,该按钮在我的 js 文件中触发 function,然后将一些值发送到 controller 并返回视图。 Using the code below, do I need to be creating a route for this?
使用下面的代码,我是否需要为此创建路线? Thanks
谢谢
view:
<div id="sendAsEmailTest">SEND AS EMAIL TEST</div>
js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "FormController/TestFunction",
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
FormController:
public function TestFunction(Request $request)
{
return view('calculator.calculator',
[
'testdata' => $request,
]
);
}
thanks for the help @ßãlãjî.感谢@ßãlãjî 的帮助。 had to adjust things slightly, but seems to be working fine now:
不得不稍微调整一下,但现在似乎工作正常:
js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "/formControllertestfunction",
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
route:
Route::post('formControllertestfunction','FormController@TestFunction')->name('formControllertestfunction');
controller:
public function TestFunction(Request $request) {
return view('calculator.back',
[
'displayName' => $request->name ,
]);
}
your route should be post你的路线应该是张贴
route::post('formController/testfunction'@'FormController@TestFunction')
just只是
public function TestFunction(Request $request)
{
dd($request->name)<---- you get your attribute value here
}
then correct
js: js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "formController/testfunction",<------------------all letters in small
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.