XHR Request

by mithril

Level: beginner • Mithril.js Version: latest

This is a very simple example that shows how to do a request using Mithril.js' m.request API method. When clicking on a button the app is doing a real XHR request and increases a counter. The example was taken from the official website at https://mithril.js.org/index.html#xhr.

Live Example


Type Name URL


<!doctype html>
<html lang=en>
  <meta charset=utf-8>


var root = document.body
var count = 0

var increment = function() {
    method: "PUT",
    url: "//rem-rest-api.herokuapp.com/api/tutorial/1",
    body: {count: count + 1},
    withCredentials: true,
    .then(function(data) {
      count = parseInt(data.count)

var Hello = {
  view: function() {
    return m("main", [
      m("h1", {
        class: "title"
      }, "My first app"),
      m("button", {
        onclick: increment
      }, count + " clicks"),

m.mount(root, Hello)

As a prerequisite for this snippet, the latest version of Mithril.js framework is required. Beginners should have no problems following this example, that simply shows some basic recipies.

Here we can see use cases of different Mithril.js API methods like m.request or m.mount, besides the centrepiece m() hyperscript function.

The example was written by mithril, last edits were made on 16 October 2021. The author has contributed some more snippets. Click here to see them all.


Did you note a typo or something else? So let me know by opening an issue. Or much better: just fork the repository on GitHub, push your commits and send a pull request. Ready to start your work? Then click on the edit link below. Thanks in advance!

See more code examples  •  Edit this example on GitHub