KnockoutJs 更新视图模型可从 Json Web 服务中观察到

KnockoutJs update view model single observable from Json webservice

本文关键字:服务 Web 观察 Json 更新 新视图 模型 KnockoutJs      更新时间:2023-09-26

新到淘汰,新到Js,我是一个点网开发人员,试图创建一个带有挖空的登录屏幕,发布数据很好,收到响应OK,只是不知道如何更新单个可观察量,请帮忙...

function AppViewModel() {
this.email = ko.observable("Bergmail.com");
this.password = ko.observable("Password");
this.logonresult = ko.observable("logon resul");
//computed
this.Computedresult = ko.computed(function () {
    return this.email() + " " + this.password();
}, this);
    //behavious
this.Loginajax = function () {
    var self=this
    $.ajax("Indi-Dal-Json.asmx/CheckLogon" ,{
        data: ko.toJSON({ email: this.email,password: this.password}),
        type: "post", contentType: "application/json",
        success: function (ajaxjsondata) {
            {
                // var parsedjson = JSON.parse(result.b);
                //AppViewModel.logonresult = (result.d);
                self.logonresult = ajaxjsondata.d;
            }
        }
    });
   };
 }
self.logonresult是一个

可观察量,所以你需要调用它,而不是用新值覆盖它的引用。

改变

self.logonresult = ajaxjsondata.d;

self.logonresult(ajaxjsondata.d);

http://knockoutjs.com/documentation/observables.html#reading_and_writing_observables

由Anders回答

self.logonresult(ajaxjsondata.d);

http://knockoutjs.com/documentation/observables.html#reading_and_writing_observables