I'm trying to fetch an object from a local api in the same project, but the product variabel is always undefined after the ajax call.
I don't get any error message from the ajax call, it returns to the success function and executes the different log commands. I thought it was an issue with decimals so I changed them to string, but same result. I've started out using $.getJSON, but changed to $.ajax just to see if that solved it.
GetProduct in _JS.js:
GetProduct: function (id) {
this.Log("JS GetProduct. Id: " + id);
$.ajax({
url: this.apiBaseUrl + "GetProduct?id=" + id,
dataType: 'json',
success: function (data) {
_JS.Log("Product fetched");
_JS.LogInfo(data);
return data;
},
error: function (xhr, status, error) {
_JS.LogError(error);
}
});
}
The call to GetProduct:
var product = _JS.GetProduct(productId);
(after this line the product is undefined, but nothing indicates why)
The json result:
{
"id": 1,
"name": "Navn pƄ norsk som er ganske langt",
"description": "Norsk forklaring",
"price": 11,
"subscriptionPrice": "9,90",
"subscriptionDiff": "1,10",
"priceFormated": "kr. 11,-",
"subscriptionPriceFormated": "kr. 9,90,-",
"subscriptionPriceDiffFormated": "kr. 1,10,-",
"isSubscription": 1
}
I don't have this problem with any other of the ajax calls to the same api (different methods).