Home » Jquery » jquery – Laravel ajax callback request response

jquery – Laravel ajax callback request response

Posted by: admin February 22, 2020 Leave a comment

Questions:
<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

How to&Answer:

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);
 }

Answer:

Try this

$(document).ready(function () {
   $("#finish").click(function(res){
    alert(1);
   });
});