title: Ajax data binding using Knockout Js
body: <p>i am using the <strong>knockout js</strong>, i am finding diffcult to bind the data while in ajax get method, i have created model, viewModel, and ajax function, i have the ajax method in the same js file where i have created viewModel i am calling the ajax on page load and trying to bind my html with konckout js, i am getting the error <code>userModel is undefined</code> if i give <code>this.name = ko.observale(result[0].name)</code> before the ajax call, after the ajax called it give <code>name is undefined</code> need help</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script src="js/jquery1.9.js"&gt;&lt;/script&gt;
      &lt;script src="js/knockout-3.3.0.js"&gt;&lt;/script&gt;
      &lt;script src="js/knockout.mapping.js"&gt;&lt;/script&gt;
      &lt;script src="model/usermodel.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
  
      &lt;div&gt;
        &lt;h1&gt;&lt;span data-bind="text:user().name"&gt;&lt;/span&gt;&lt;/h1&gt;
        &lt;h1&gt;&lt;span data-bind="text:user().userName"&gt;&lt;/span&gt;&lt;/h1&gt;
      &lt;/div&gt;
      &lt;script src="ViewModel/userDetailsViewModel.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  
  
  <pre><code>////Model////
  function userModel(result) {
    var self = this;
    this.name = ko.observable(result[0].name); /// give me error undefined before  the ajax call and after ajax call i get the value in result
    this.userName = ko.observable();
  
  }
  
  /////View Model////
  var result
  var userDetailsViewModel = function(result) {
    self = this;
    self.user = ko.observable(new userModel(result));
  };
  var mainUserDetailsViewModel = new userDetailsViewModel(result);
  ko.applyBindings(mainUserDetailsViewModel);
  
  ////ajax called on the page load ////
  $.ajax({
    type: "POST",
    dataType: "json",
    url: baseUrl + 'api/xx/xxx',
    data: jason.strigfy(),
    success: function(data) {
      result = data;
      ////I am getting in result json data object 0=["name":"nnnn","Username":"mmmmmm"],
      ////  i am passing this result to ViewModel and to Usermodel Constructor//
      mainUserDetailsViewModel.user(new userModel(result));
  
    },
    error: function(error) {
      jsonValue = jQuery.parseJSON(error.responseText);
      //jError('An error has occurred while saving the new part    source: ' + jsonValue, { TimeShown: 3000 });
    }
  });
  </code></pre>