I have a form that posts to a remote API. The form works fine but I need to populate the options in the select boxes with an Ajax call.
This is what I have so far but clueless on the next step.
form.html
<form data-bind="submit: mySubmit" role="form">
<select data-bind="
options: operative,
optionsValue: 'id',
optionsText: function(i) {
return i.FirstName + ' ' + i.LastName
},
optionsCaption: 'Choose...'
"></select>
<select data-bind="
options: claim,
optionsValue: 'id',
optionsText: function(i) {
return i.id
},
optionsCaption: 'Choose...'
"></select>
<textarea data-bind="value: body" cols="23" rows="5"></textarea>
<input data-bind="value: entryDate" type="text">
<button type="submit">Go</button>
</form>
javascript
var viewModel = {
operative: ko.observable(),
claim: ko.observable(),
body: ko.observable(),
entryDate: ko.observable(),
operative: [{}],
claims: [{}],
mySubmit : function(formElement) {
var formData = {
'operative' : viewModel.firstname() ,
'claim' : viewModel.lastname(),
'body' : viewModel.firstname() ,
'entryDate' : viewModel.lastname()
};
console.log(formData)
$.ajax({
url: '/api/entry/',
type: "POST",
data: JSON.stringify(formData),
datatype: "json",
processData:false,
contentType: "application/json; charset=utf-8",
success: function (result){
alert("success");
}
});
}
};
ko.applyBindings(viewModel);