简体   繁体   中英

AJAX - Sending knockout observables as JSON object to server using AJAX

I am trying to send form fields which are bound to particular observable to my server in the form of JSON object but I receive empty JSON string at server side. I do not want to send the entire view model to accomplish this task. this is the javascript i have so far:

 $(document).ready(function(){
    ko.applyBindings(new AddSubjectKo());
});



  function AddSubjectKo()
  {
    var self=this;
    self.name = ko.observable();
    self.quiz = ko.observable();
    self.ass = ko.observable();
    self.oht = ko.observable();
    self.sess = ko.observable();
    self.ese = ko.observable();
    self.SubjectAdded=function()
    {
        $.ajax({
            url: "api/courses",
            type: "post",
            data: formToJSON(),
            contentType: "application/json",
            success: function(data){
           alert("success");
             },
          error:function(jqXHR, textStatus, errorThrown) {
               alert("failure");
             }   
       });  
        function formToJSON() {
            alert(self.name());
               return JSON.stringify({
                   "name": self.name,
                   "quiz": self.quiz,
                   "ass": self.ass,
                   "oht": self.oht,
                   "sess": self.sess,
                   "ese": self.ese,
                    });
           }
        }
      //$("#alert").slideDown();

  }

You can use ko.toJSON function for this:

  function AddSubjectKo()
  {
    var self=this;
    self.name = ko.observable();
    self.quiz = ko.observable();
    self.ass = ko.observable();
    self.oht = ko.observable();
    self.sess = ko.observable();
    self.ese = ko.observable();
    self.SubjectAdded=function()
    {
        $.ajax({
            url: "api/courses",
            type: "post",
            data: ko.toJSON(self),
            contentType: "application/json",
            success: function(data){
                alert("success");
             },
             error:function(jqXHR, textStatus, errorThrown) {
                alert("failure");
             }   
       });  
   }

Just use call the observable (add parenthesis) to grab the values in the observables:

 $(document).ready(function(){
    ko.applyBindings(new AddSubjectKo());
});



  function AddSubjectKo()
  {
    var self=this;
    self.name = ko.observable();
    self.quiz = ko.observable();
    self.ass = ko.observable();
    self.oht = ko.observable();
    self.sess = ko.observable();
    self.ese = ko.observable();
    self.SubjectAdded=function()
    {
        $.ajax({
            url: "api/courses",
            type: "post",
            data: formToJSON(),
            contentType: "application/json",
            success: function(data){
           alert("success");
             },
          error:function(jqXHR, textStatus, errorThrown) {
               alert("failure");
             }   
       });  
        function formToJSON() {
            alert(self.name());
               return JSON.stringify({
                   "name": self.name(),
                   "quiz": self.quiz(),
                   "ass": self.ass(),
                   "oht": self.oht(),
                   "sess": self.sess(),
                   "ese": self.ese(),
                    });
           }
        }
      //$("#alert").slideDown();

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM