使用数据在 Javascript 中重定向(laravel 5.3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40650392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect in Javascript with data (laravel 5.3)
提问by Ramzan Mahmood
I have to redirect on edit page with its data if user clicks on save button here is my button:
如果用户单击保存按钮,我必须使用其数据重定向编辑页面,这里是我的按钮:
<button type="submit" name="edit" class="btn green" value="edit">Save</button>
When user click this button i get name of button here in update controller as
当用户单击此按钮时,我在更新控制器中获取按钮的名称为
return $request->input('edit');
this return goes in to js file
此返回进入 js 文件
success: function(result) {
if (result == 'save_add') {
window.location = "/student-management/list/create"
} else if (result == 'edit') {
window.location = "////////////////////////////"
}else {
window.location = "/student-management/list"
}
}
route is
路线是
route('student.edit')
Here i am redirecting user to its actual path in case of edit on 2nd else if i want to redirect on edit page with its data i am working same here with other page in controller but i want to do this in js
在这里,我将用户重定向到其实际路径,以防在 2nd 上进行编辑,否则,如果我想使用其数据在编辑页面上重定向,我在这里与控制器中的其他页面工作相同,但我想在 js 中执行此操作
if(Input::get('save_add')) {
return redirect()->route('student.create');
} elseif(Input::get('edit')) {
return redirect()->route('student.edit', ['id' => $id ]);
} else {
return redirect()->route('student.index');
}
I want to send same as above id with that redirect for edit
我想发送与上述 id 相同的重定向以进行编辑
回答by Joe
Assuming your JavaScript already knows the user's id in a variable called id
, and the edit url to send to takes the form of /student-management/student/{id}
:
假设您的 JavaScript 已经在名为 的变量中知道用户的 id id
,并且要发送到的编辑 url 采用以下形式/student-management/student/{id}
:
window.location = "/student-management/student/" + id
回答by Vikash
You can try this
你可以试试这个
In your controller
在您的控制器中
return return response()->json(['success'=>true,'result'=>$request->input('edit'),'url'=> route('student.edit', ['id' => $id ])]);
Your js
你的js
success:: function(data)
{
if(data.success)
{
window.location = data.url
}
}
Hope this will help
希望这会有所帮助