gpt4 book ai didi

javascript - 如何将成功函数名称作为函数参数传递给 AJAX 调用?

转载 作者:行者123 更新时间:2023-11-29 23:19:01 24 4
gpt4 key购买 nike

在这里,我尝试将 AJAX 调用作为单个函数进行,因为我将成功函数名称作为函数参数传递给 AJAX 调用函数。

我尝试编写以下函数:

function ApiCallFunction(Datatext, ApiName, FunctionName) {
$.ajax({
url: Apiurl + ApiName,
type: "POST",
data: Datatext,
contentType: "application/json",
dataType: "json",
success: function(data) {
var funname = FunctionName + '("' + data + '")';
eval(funname);
},
error: function(error) {
jsonValue = jQuery.parseJSON(error.responseText);
ErrorWhileSave(jsonValue.Message);
},
failure: function(response) {
ErrorWhileSave("");
}
});
}

函数调用:

var datatext = {
BillChild: {},
BillDate: "2018-07-23T08:35:32.319Z",
EntryTime: "2018-07-23T08:35:32.319Z",
ExitTime: "2018-07-23T08:35:32.319Z",
TotalTime: "2018-07-23T08:35:32.319Z",
Total: 0,
OtherCharges: 0,
Discount: 0,
TaxableAmount: 0,
TotalTax: 0,
GrandTotal: 0,
RoundOff: 0,
NetAmount: 0,
ByCash: 0,
ByBank: 0,
CashReceived: 0,
BalanceReceivable: 0,
FirmId: 0,
UserId: 0,
BillId: 35,
CustomerId: 0,
BranchId: 0,
BillType: "string",
BillNo: "string",
PaymentType: "string",
Notes: "string",
TaxType: "string",
BankId: "string",
CreatedBy: "string",
HostIp: "string",
BranchTransfer: "string",
ConsultId: 0,
SearchKey: "string",
Flag: "SELECTONE"
};

var Datatext = (JSON.stringify(datatext));
ApiCallFunction(Datatext, "Bill_master", "ReturnFunction");

我尝试使用的 Success 函数是:

function ReturnFunction(ReturnValue) {
alert(data.data.Table1[0].BillId);
}

当我尝试 alert(ReturnValue) 时,它显示为 object object。我还尝试了 ReturnValue.data.data.Table1[0].BillId 仍然无法使用这些值。 AJAX 调用成功,我从中获得了值(value),但我无法将结果 JSON 对象传递给其他函数。

如何将 JSON 对象传递给其他函数?请帮助我。

最佳答案

您可以通过不同的方式实现您想要做的事情。

Method 1 simply assigning the function object as the parameters

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);

Method 2 if you happen to have a string instead of the function object

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");

关于javascript - 如何将成功函数名称作为函数参数传递给 AJAX 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com