Reputation: 21
Here I am trying to make AJAX call as a single function, for that I am passing success function name as a function parameter to the AJAX call function.
I tried writhing the following function:
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("");
}
});
}
Function call:
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");
And the Success function am trying to use is:
function ReturnFunction(ReturnValue) {
alert(data.data.Table1[0].BillId);
}
When I tried alert(ReturnValue)
it showed as object object
. I also tried ReturnValue.data.data.Table1[0].BillId
still am not able to use the values. AJAX call is success and I am getting value in it, but I am unable to pass the result JSON object to other function.
How can I pass JSON object to other function? Please help me.
Upvotes: 2
Views: 2329
Reputation: 1313
There are different ways by which you can achieve what you are trying to do.
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("");
}
});
}
and have the implementation of the function as:
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);
}
calling:
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("");
}
});
}
Calling:
ApiCallFunction(DataText,"Bill_master","ReturnFunction");
Upvotes: 2
Reputation: 1279
In the ReturnFunction, i think data is undefined, maybe this should work instead :
function ReturnFunction(ReturnValue) {
alert(ReturnValue.data.Table1[0].BillId);
}
Upvotes: 0
Reputation: 24156
You can pass function address (callback):
function ApiCallFunction(Datatext, ApiName, FunctionName) {
$.ajax({
url: Apiurl + ApiName,
type: "POST",
data: Datatext,
contentType: "application/json",
dataType: "json",
success: function (data) {
FunctionName(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
ErrorWhileSave(jsonValue.Message);
},
failure: function (response) {
ErrorWhileSave("");
}
});
}
function ReturnFunction(data) {
alert(data.data.Table1[0].BillId);
}
ApiCallFunction(Datatext, "Bill_master", ReturnFunction);
Upvotes: 0