Search code examples
phpjqueryajaxlaravelurl-routing

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') }}";?


Solution

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