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 bi开发者_开发问答t clear if not feel free to ask.
Thank you for your time.
Kind regards, Mark
- This won't work
- 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
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);
精彩评论