Firstly, there appears to be a couple syntax errors in your code near the bottom. I don't think you've closed the $.ajax() call, nor the object inside of it.
Secondly, it appears as though you are trying to return dataBKP after the success function fires, however you've written it outside of your success function.
The success callback runs asynchronously, and therefore after ajax has completed. The return statement here will fire before success has a chance to run in most circumstances.
The solution would be to move it into the success function:
var dataBKP;
$.ajax({
type: 'POST',
dataType: 'json',
contentType: "application/json",
cache: false,
url: '@Url.Action("GetBpkOtrAsync", "SimulasiKredit")',
data: JSON.stringify(param),
success: function (Data, textStatus, jqXHR) {
$.each(Data.data, function (i, rowData) {
dataBKP = Number(rowData.rateBPK);
});
return dataBKP;
}
});
You might find it easier to track which functions you've closed if you add spacing inside the nested components, as well, so you don't end up with }}); at the end.
Lastly, you're running an .each loop over your data, however overwriting the dataBKP var on each pass. I would recommend either concatenation, summation, or pushing into an array. (dataBKP = dataBKP + Number(rowData.rateBPK);, for example).
EDIT: after seeing you make a couple changes, I'm guessing your post is still in flux. If so, then you may already know all the things I've listed above and you're trying to correct them. In that case, I want to add that you are probably going to bump into a couple things:
First, you're returning the dataBKP var, instead of using it. This is kind of a confusing topic since they load asynchronously, but you are probably returning it to nowhere. The success function is a void function. Instead, try logging the success function so you can see it appearing later and get an understanding of how this works.
Next, dataBKP being set will be hard to play with because of scope.
Add a function that the success action can call so that your code can use the dataBKP var. And then, consider passing it in so that function can be reused later. And then pass the value, rather than directly calling the variable. Like so:
var dataBKP = 0;
var functionToManipulateThatDataAfterAjaxCall = function(data) {
// do something with your data here
console.log("Called from within the success callback: " + dataBKP);
};
console.log("Just before the success callback: " + dataBKP);
$.ajax({
type: 'POST',
dataType: 'json',
contentType: "application/json",
cache: false,
url: '@Url.Action("GetBpkOtrAsync", "SimulasiKredit")',
data: JSON.stringify(param),
success: function (Data, textStatus, jqXHR) {
$.each(Data.data, function (i, rowData) {
dataBKP = Number(rowData.rateBPK);
});
console.log("Inside the success callback: " + dataBKP);
functionToManipulateThatDataAfterAjaxCall(dataBKP);
}
});
console.log("Just after the success callback: " + dataBKP);