【问题标题】:Knockout JS Ajax call of jsonp not populating observable arrayjsonp的Knockout JS Ajax调用未填充可观察数组
【发布时间】:2013-08-13 18:03:52
【问题描述】:

页面加载事件后,如果更改下拉列表中的值,则它会成功显示警报并进行 jquery ajax 调用以从 WebApi 返回数据。但是数据不绑定到 UL。下面是代码。

<ul data-role="listview" data-divider-theme="b" data-inset="true" data-bind="foreach: Contacts" style="margin-top:-17px;">
 <li data-theme="c">
 <a href="#page1" data-transition="slide" data-bind="attr: { title: ContactID }">
 <span data-bind="text: FirstName + ' ' + LastName ')'"></span>
 </a>
 </li>
</ul>
function StrikeAppViewModel() {
    var self = this;

    self.Contacts = ko.observableArray();

        self.SearchContacts = function () {
        alert("Onchange event is fired");                  
        $.ajax({
            url: 'http://localhost:50043/api/Contacts',
            type: 'GET',
            dataType: 'jsonp',
            context: this,
            success: function (result) {
                debugger;
                self.Contacts($.parseJSON(result));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });         
    }
   };

我不知道为什么数据没有绑定到ul。它在网页中没有显示任何错误。

请告诉我哪里出错了。

【问题讨论】:

    标签: jquery knockout.js single-page-application


    【解决方案1】:

    请改写这一行。然后就可以正常使用了

      self.Contacts(result);
    

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 2012-03-20
      • 2018-09-08
      • 2014-01-28
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多