Chapter 8. Server-Side Interaction
Knockout doesn’t include any functions to perform server-side interaction. It is completely up to you. You can use jQuery, vanilla JavaScript, or any other library that you prefer.
I suggest using jQuery because some of the previous examples are already using it, which means it is most likely already included in your project, thus adding no additional override.
Sending Data
When it comes to sending data, I typically use a few different formats depending on whether I’m interacting with a REST API or if I’m POSTing a standard form. There are two major differences when changing between types:
- The
content-type
of the AJAX request is different. The default form when performing an AJAX request using jQuery is application/x-www-form-urlencoded. This would be used for a standard form POST. A typicalcontent-type
when working with a REST API would beapplication/json
when working in JSON. - The content of the form is also different. When performing a standard form POST, the data would be serialized as a standard JavaScript key/value pair of data. Whereas, when interacting with a JSON REST API, the form data would be serialized to JSON. Knockout does provide helpful functions to unwrap observables and convert them to the necessary types that will be explored in the upcoming examples.
In Example 8-1, I will create a standard form that leverages several Knockout bindings. When the form is submitted, it will be done via AJAX using the standard form POST. ...
Get Knockout.js now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.