1 回答

TA貢獻2039條經驗 獲得超8個贊
您可以通過不同的方式實現您的目標。
方法一簡單地將函數對象作為參數賦值
function ApiCallFunction(Datatext, ApiName, onSucess,onError) {
$.ajax({
url: Apiurl + ApiName,
type: "POST",
data: Datatext,
contentType: "application/json",
dataType: "json",
success: onSucess,
error: onError,
failure: function (response) {
ErrorWhileSave("");
}
});
}
并具有以下功能的實現:
function ReturnFunction(response){
//assuming that response is of JSON type
alert(response.data.Table1[0].BillId);
}
function myError(response){
console.log(JSON.parse(response.responseText).Message);
}
調用:
ApiCallFunction(DataText,"Bill_master",ReturnFunction,myError);
方法2如果你碰巧有一個字符串而不是函數對象
function ApiCallFunction(Datatext, ApiName, FunctionName) {
$.ajax({
url: Apiurl + ApiName,
type: "POST",
data: Datatext,
contentType: "application/json",
dataType: "json",
success: function (data) {
window[FunctionName].apply(this,data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
ErrorWhileSave(jsonValue.Message);
},
failure: function (response) {
ErrorWhileSave("");
}
});
}
調用:
ApiCallFunction(DataText,"Bill_master","ReturnFunction");
- 1 回答
- 0 關注
- 172 瀏覽
添加回答
舉報