user8012596
user8012596

Reputation: 854

Laravel after ajax sucesss calling another controller with data

Currently I am able to pass my form data via ajax to my controller to insert into db. This works fine and after insert I will get data from the controller and pass to another controller only after this ajax is success. Currently stucked here cause I am using this method 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') }}";
}

In the paysubmit controller currently I am using this method return view('cds')->with($data); but I need to do this in this ajax cause this codes are in php? I need to improvise on this window.location.href = "{{ URL::asset('/cds') }}";?

Upvotes: 1

Views: 2267

Answers (2)

Rahul
Rahul

Reputation: 2474

You can use any of the two ways to solve your issue:

  • 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.

What you are trying to achieve is not legit as you are returning a view to ajax success which is basically a html , and once you redirect to another url you will lost your data.

Upvotes: 1

Nauman Zafar
Nauman Zafar

Reputation: 1103

You can write just another call within the success callback of your first ajax request. And use data received from your first call within your second ajax call.

$.ajax({
    method: "POST",
    url: "{{ '/home/view' }}",
    data: { eID:eID},
    success: function(data){
       if (data){
           $.ajax({
               method="POST",
               //other ajax settings here.
           })
       }
});

Upvotes: 1

Related Questions