Show More
Show More

Reputation: 45

Laravel ajax callback request response

<div class="col-xs-12 col-sm-12 col-md-12 text-center ">
      <button  id="finish" class="btn btn-primary">finish</button>
 </div>

//script

 $('#addNew').click(function(cb){

    $.ajax({
        type:GET;
        url:'Get/local/'
        dataType:'json',
        data:form_data,
        success:function(res){ 
            cb(res); 
        }
    });

//and now I want to pass res I get to other function, and I try this, but not working "data" get from the first function// the first function res

$('#finish').click(function(data){
    console.log(data);
});

any help, please. res..give me data of an object. my problem is I am new to ajax and I don't know how to a callback res to other function

Upvotes: 2

Views: 102

Answers (1)

Edwin Krause
Edwin Krause

Reputation: 1806

One way would be to store the data in a global variable

var data_global = {};

$('#addNew').click(function(){

  $.ajax({
   type:GET;
   url:'Get/local/'
   dataType:'json',
   data:form_data,
   success:function(res){ 
     data_global = res; 
   }
 });

Then you can pick up the data stored in the global variable with a function or an event on another button

 $('#finish').click(function(){
    console.log(data_global);
 });

Depending on your logic, usually you would call a function inside the success callback

$('#addNew').click(function(cb){

  $.ajax({
   type:GET;
   url:'Get/local/'
   dataType:'json',
   data:form_data,
   success:function(res){ 
     myFantasticFunction(res); 
   }
 });

 myFantasticFunction(data){
   console.log(data);
 }

Upvotes: 2

Related Questions