I return this json from the server:
{
"Id": 0,
"Name": ko.observable('New Product'),
"PurchasePrice": 0.0,
"DownPayment": 0.0,
"TotalPayment": 0.0,
"MortgageInsurance": 0.0,
"PurchaseOrRefinance": null,
"Client": null,
"MonthlyCosts": null,
"ClosingCosts": null,
"FirstLien": null,
"SecondLien": null
}
I retrieve it using jQuery.getJson like this:
function addProduct(){
$.getJSON('@Url.Action("GetNewProduct","Product")',function(data){
viewModel.products.push(data);
});
viewModel.productSaved(false);
viewModel.product(products[products.length - 1]);
}
However, it seems as if the code never runs. This only happends when I return the function in the json, if I remove the function everything runs fine. Is there a way to work this out?
Parsing your data fails since
isn’t actually JSON. You can pass only basic data such as strings/numbers/objects via JSON.
See http://www.json.org/
You may consider calling jQuery.ajax() with dataType set to ‘script’.
Otherwise you need to initialize your variables within your callback function or in the server code, which might even be a cleaner aproach.