我从服务器返回这个 json:
{
"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
}
我使用 jQuery.getJson
检索它,如下所示:
function addProduct(){
$.getJSON('@Url.Action("GetNewProduct","Product")',function(data){
viewModel.products.push(data);
});
viewModel.productSaved(false);
viewModel.product(products[products.length - 1]);
}
但是,代码似乎从未运行过。仅当我在 json 中返回该函数时才会发生这种情况,如果删除该函数,一切都会正常运行。有没有办法解决这个问题?
最佳答案
应该是这样的>
返回此
{"Id":0,"Name":'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}
像这样>
function addProduct(){
$.getJSON('@Url.Action("GetNewProduct","Product")',function(data){
data.Name = ko.observable(data.Name);
viewModel.products.push(data);
});
viewModel.productSaved(false);
viewModel.product(products[products.length - 1]);
}
关于jquery - 如何在 json 属性中执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254982/