1

I hope someone can help me with the following scenario because I'm breaking my small head over this! I have made a wrapper so I can easily call webservices/pagemethod using jquery. Now in the success function of the ajax call I want to be able to call a function (with optional parameters) I have done the following but I'm not sure if I'm the right direction:

function AjaxMethod(path, fn, paramArray, returnParamArray, successFn, errorFn) {

var paramList = '';
if (paramArray.length > 0) {
    for (var i = 0; i < paramArray.length; i += 2) {
        if (paramList.length > 0) paramList += ',';
        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
    }
}

paramList = '{' + paramList + '}';

if (returnParamArray.length > 0) {
    $.ajax({
        type: "POST",
        url: path + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function () {
            var strFun = successFn;
            var strParam = returnParamArray;
            var funcCall = strFun + "('" + strParam + "');";
            //Call custom function with parameter array
            var ret = eval(funcCall);
        },
        error: errorFn
    })
}
else {
    $.ajax({
        type: "POST",
        url: path + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    })
}

;}

I am able to call the function but I cant seem to pass the parameters. Am I going in the right direction or is this complete off the path ? Is it even possible to pass parameters/values as an array. Or is there an easier way to accomplish this.

By the way I need this function to make up the following (Schematic) actions:

1) User deletetes a row from a table with data - DeleteRow(rowindex)

2) DeleteRow calls the AjaxMethod to delete the row from database

3) On success event I want the specific row to be deleted visually, so I need the selected rowindex available in the successfunction. There are a couple of other scenarios too so it needs to be really dynamic.

I can also store this information in global vars like CurrentDeletedIndex but I hope it's possible in the way I was thinking.

I hope I made myself a little bit clear if not feel free to ask.

Thank you for your time.

Kind regards, Mark

Mark
  • 139
  • 1
  • 9

2 Answers2

0

You need to use the apply method

So instead of using eval, and trying to create a String representation of parameters use this

window[successFn].apply(null, paramArray);
Community
  • 1
  • 1
Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
0
  1. This won't work
  2. This is bad; eval is evil

The proper way to do this is fairly simple. First, we need to make the object paramList, not its string representation. We can do it like this:

var paramList = {};
for(var i=0; i<paramArray.length; i+=2) {
    paramList[paramArray[i]] = paramArray[i+1];
} // note: make sure the array size is even.

Now for the success function:

var ret = successFn(paramList);

NOTE: if successFn is passed as a string, use window[successFn] for the global scope, or window.some.scope[successFn] for a specific scope, and I wholeheartedly suggest you to take the actual function as a parameter, not some string. This is useful for anonymous functions and functions from unreachable scopes. Oh and it's also the proper way of doing things

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
  • successFn is being passed in as a string. This will not work. – Josiah Ruddell Nov 09 '10 at 20:17
  • Thank you Gabi en Josiah for your clarification, the success function is working now but how do I access the result (both ajax result AND the return paramlist) in the successfunction. Sorry I'm still learning but thank you very much! – Mark Nov 10 '10 at 13:33
  • @Mark please update the question with more detailed information and your if you are still in trouble – Gabi Purcaru Nov 10 '10 at 13:38