0

jQuery - ajax success parameter I want to know how to give a object method (function), on the success parameter, to the ajax function (sorry for my English)

lets say I already set my ajax.setup with all I need to call a PHP on the server.

function myClass(id)
{
  this.id = id;

  this.showValues=function(xml) 
  {
    alert(this.id); // to prove we r in the right object
    alert(typeof(xml)); // to prove we got the xml     
  };

  this.retrieveValues=function()
  { 
    // wont call the method, this is just text, like writing sucess:"hello"
    $.ajax({success: this.id+'.showValues'}); 


   // calls the method but this.id is undefined (xml is received though)
    $.ajax({success: this.showValues}); 


    // wont call the method
    $.ajax({success: new Function(this.id+'.showValues')}); 


    // calls the method with the right id but xml is undefined
    $.ajax({success: new Function(this.id+'.showValues()')}); 


    // js error - "xml not defined"
    $.ajax({success: new Function(this.id+'.showValues(xml)')}); 


    // of course, next line works, but I don't want to define the function here
     $.ajax({success: function() { alert(this.id); alert(typeof(xml)); } });    
     
  };

}


// even declaring the object as global, so the class has a chance to call a method using the var name
// which is this.id+'.retrieveValues' ==> object1.retrieveValues
var object1;

function Main() // main =d
{
  object1 = new myClass('object1');
  object1.retrieveValues();

}

again, sorry for my bad English .. learning still. I hope someone can help me, thank you

1 Answer 1

1

found it using Function api help <_< i was missing the "xml" parameter

$.ajax({success: new Function("xml",this.id+'.showValues(xml)')}); 

new Function("xml",this.id+'.showValues(xml)') is like saying

function(xml) { object1.showValues(xml); }

thanks dedoz. no problem. =D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.