为什么这个getJSON调用返回undefined?

时间:2014-01-09 21:31:05

标签: jquery json

我在内部服务器上运行JSON服务,该服务返回以下记录:

    {
    "getLTCWithIDsResult": {
        "AIMSubmissionID": "",
        "BrokerName": "",
        "DateApplied": "/Date(1389302726241-0600)/",
        "Dirty": false,
        "EffectiveDate": "/Date(1389302726241-0600)/",
        "ExpiringPremium": 0,
        "GrandTotal": 0,
        "Insured": {
            "Address1": "",
            "Address2": "",
            "City": "",
            "County": "",
            "InsuredName": "Nice Try Bro",
            "MailAddress1": "",
            "MailAddress2": "",
            "MailCity": "",
            "MailCounty": "",
            "MailState": "",
            "MailZip": "",
            "Phone1": "",
            "Phone2": "",
            "State": "",
            "Zip": ""
        },
        "IsRenewal": false,
        "Locations": [ ],
        "PercentChange": 0,
        "PolicyID": "",
        "QuoteID": 0,
        "QuoteVersion": 0,
        "RetroDate": null,
        "Status": null
    }
}

我正在尝试使用以下脚本将此记录的一部分放到HTML页面上。不幸的是,success:function(data)行中的数据变量不断返回“undefined”。任何人都可以得到一些帮助吗?

$(document).ready(function ()
{
    var appViewModel 
    // AppViewModel
    function AppViewModel()
    {
        this.InsuredName = ko.observable();
    }
    var appViewModel = new AppViewModel();
    ko.applyBindings(appViewModel);

    $.getJSON("http://waltweb01:85/LTCEPLWS/LTCJSON.svc/getLTCWithIDs/'4'",
    {
        success: function (data)
        {
            incomingData = data;
            appViewModel.InsuredName(incomingData.InsuredName);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

那是你如何使用$.getJSON。您只能在使用success: function()时使用$.ajax。使用速记方法($.getJSON$.post$.get)时,您只需传递函数而不是对象。

$.getJSON("http://waltweb01:85/LTCEPLWS/LTCJSON.svc/getLTCWithIDs/'4'", function(data){
    incomingData = data;
    appViewModel.InsuredName(incomingData.InsuredName);
});