Home » Javascript » Sending data of ajax by another ajax to controller in laravel

Sending data of ajax by another ajax to controller in laravel

Posted by: admin August 19, 2018 Leave a comment


I want to know is that possible to send data from one ajax by another ajax or not?

Sounds confusing I know, but here is explanation:

I have payment method where it gets data and handling them by Ajax (unfortunately the creators of this API limited their code a lot) so even if i try to add input request in controller code of that Ajax nothing will work, that’s why I need to make another Ajax to handle that input request.

Lets explain more by codes:


public function orderspayonline(Request $request, $id){
        error_log('masuk ke snap token dri ajax');
        $midtrans = new Midtrans;
        //products data + user info etc.

        //here magic happens
            $snap_token = $midtrans->getSnapToken($transaction_data);
            echo $snap_token;
        catch (Exception $e) 
          return $e->getMessage;

If I add anything (I mean anything) in that try{ part it will stop functioning and return error! `even I tried to redirect back my users except echoing token code that gave error as well. So it seems I really don’t have any option here but to create new function and Ajax.


<script type="text/javascript">
  $('.pay-button').click(function (event) {

        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
    // $(this).attr("disabled", "disabled");
    var prdfoId = $(this).data('id');
          url: '{{url("orderspayonline")}}/'+encodeURI(prdfoId),
          type: "POST",
          cache: false,

          success: function(data) {
            var resultType = document.getElementById('result-type');
            var resultData = document.getElementById('result-data');

            function changeResult(type,data){

            snap.pay(data, {
              onSuccess: function(result){
                changeResult('success', result);
              onPending: function(result){
                changeResult('pending', result);
              onError: function(result){
                changeResult('error', result);

The part I need to manipulate is snap.pay(data, { where results gets back.

Currently they are return in console and disappear in a sec as the result of echo $snap_token; in my controller.
I have tried to get them in hidden input, but as I mentioned I cannot get results because I can’t change my try code, even I tried to get them after catch part closed, the same thing happens Error.


  1. How can I get my results in controller?

I need to update my database with that results.