Laravel 在 ajax 成功调用另一个带有数据的控制器之后
Laravel after ajax sucesss calling another controller with data
目前我可以通过 ajax 将我的表单数据传递到我的控制器以插入到数据库中。这工作正常,插入后我将从控制器获取数据并仅在此 ajax 成功后传递给另一个控制器。目前卡在这里因为我正在使用这种方法 window.location.href.
$.ajax({
method: "POST",
url: "{{ URL::asset('paysubmit') }}",
data: {eID:eID,amount:amount,"_token": "{{ csrf_token() }}" },
success: function(data) {
window.location.href = "{{ URL::asset('/cds') }}";
}
目前我在 paysubmit 控制器中使用此方法 return view('cds')->with($data);
但我需要在 ajax 中执行此操作,因为此代码在 php 中?我需要即兴发挥 window.location.href = "{{ URL::asset('/cds') }}";
?
您可以在第一个 ajax 请求的 success
回调中编写另一个调用。并在您的第二个 ajax 通话中使用从您的第一个通话收到的 data
。
$.ajax({
method: "POST",
url: "{{ '/home/view' }}",
data: { eID:eID},
success: function(data){
if (data){
$.ajax({
method="POST",
//other ajax settings here.
})
}
});
您可以使用以下两种方法中的任何一种来解决您的问题:
Make another call inside your ajax success function and pass the data received from your first call to second call.
In your first controller method after you get the data, call second controller method and then return the result.
你试图实现的是不合法的,因为你正在返回 ajax success 的视图,这基本上是一个 html ,一旦你重定向到另一个 url 你会丢失了您的数据。
目前我可以通过 ajax 将我的表单数据传递到我的控制器以插入到数据库中。这工作正常,插入后我将从控制器获取数据并仅在此 ajax 成功后传递给另一个控制器。目前卡在这里因为我正在使用这种方法 window.location.href.
$.ajax({
method: "POST",
url: "{{ URL::asset('paysubmit') }}",
data: {eID:eID,amount:amount,"_token": "{{ csrf_token() }}" },
success: function(data) {
window.location.href = "{{ URL::asset('/cds') }}";
}
目前我在 paysubmit 控制器中使用此方法 return view('cds')->with($data);
但我需要在 ajax 中执行此操作,因为此代码在 php 中?我需要即兴发挥 window.location.href = "{{ URL::asset('/cds') }}";
?
您可以在第一个 ajax 请求的 success
回调中编写另一个调用。并在您的第二个 ajax 通话中使用从您的第一个通话收到的 data
。
$.ajax({
method: "POST",
url: "{{ '/home/view' }}",
data: { eID:eID},
success: function(data){
if (data){
$.ajax({
method="POST",
//other ajax settings here.
})
}
});
您可以使用以下两种方法中的任何一种来解决您的问题:
Make another call inside your ajax success function and pass the data received from your first call to second call.
In your first controller method after you get the data, call second controller method and then return the result.
你试图实现的是不合法的,因为你正在返回 ajax success 的视图,这基本上是一个 html ,一旦你重定向到另一个 url 你会丢失了您的数据。