ssharma
ssharma

Reputation: 349

Passing parameters through Ajax success function

I have a ajax function:

function abc(url){    
$.ajax({
                url:'/account/search',
                type:'GET',
                success:update(url),
                error:function(error){
                    console.log(error);
                }
            });
}

On success,there is a function call to update:

   function update(response,url){
            list = response.data;
            var i=0;
            var q = list.length;
}

But it throws the error:Cannot read property 'length' of undefined in console.

As update function is already receiving data from an another file,which comes in the parameter response,so I can't remove it.

Please provide suggestions how to correctly pass a parameter(i.e. the "url" in this example) to the update function from AJAX call.

Thanks

Upvotes: 3

Views: 12583

Answers (3)

Lucky
Lucky

Reputation: 575

Try this: It may work in this scenario. Bind the value using this may work.

   
   
     function addRemoveQuestion(id){
        
         
         var formData =    $("#q_"+id).serialize()  
         
        $.ajax({
            method: "POST",
            url: window.location.href,
            data: formData,
            success: handleFormSuccess.bind(this, id) ,
            error: handleFormError,
        })
   }
   function handleFormSuccess( data, response,textStatus, jqXHR){
        console.log( data)//id
        console.log(textStatus)
        console.log(jqXHR)
        console.log("#btn_"+data )
      
        
    }

Upvotes: 0

Vinaysingh Khetwal
Vinaysingh Khetwal

Reputation: 121

Try This Do not pass any parameter in success

function abc(url)
{    
  $.ajax({
                url:'/account/search',
                type:'GET',
                success: update,
                error:function(error){
                    console.log(error);
                }
            });
}



function update(data){
            console.log(data)
            var q = data.length;
}

Upvotes: 0

Sirko
Sirko

Reputation: 74036

You'll need another function. The success callback itself just received one parameter: the AJAX response. If you need a second one, you'll need a function call in between.

function abc(url){    
  $.ajax({
                url:'/account/search',
                type:'GET',
                success: function(data){ update(data, url); },
                error:function(error){
                    console.log(error);
                }
            });
}

Upvotes: 11

Related Questions