jquery - 如何在 json 属性中执行函数?

标签 jquery json knockout.js

我从服务器返回这个 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/

相关文章:

javascript - 使用 Jquery 选择正确的绑定(bind)按钮

javascript - 在内部单击时保持 Bootstrap 下拉菜单打开

jquery - HTML 5 本地计算机硬件规范

c++ - Jsoncpp问题

php - MYSQL/JSON 输出计数和百分比

javascript - 通过 attr 数据绑定(bind)的 i18n 文本未显示

javascript - 如何在knockout js中的两个 View 模型之间交换值

javascript - 我不知道如何按顺序放置 block (或按钮)。(上下)

data-binding - 绑定(bind)到链中的可观察对象可以为空的嵌套属性

javascript - 如何验证 g-captcha-response?