Ovto.fetch

Ovto provides wrapper of Fetch API, for convenience of calling typical server-side APIs (eg. those generated by rails scaffold command.)

Ovto.fetch returns Opal's Promise object that calls the API with the specified parameters.

Examples

GET

Ovto.fetch('/api/tasks').then{|json_data|
  p json_data
}.fail{|e|  # Network error, 404 Not Found, JSON parse error, etc.
  p e
}

POST

Ovto.fetch('/api/new_task', 'POST', {title: "do something"}).then{|json_data|
  p json_data
}.fail{|e|  # Network error, 404 Not Found, JSON parse error, etc.
  p e
}

PUT

Ovto.fetch('/api/tasks/1', 'PUT', {title: "do something"}).then{|json_data|
  p json_data
}.fail{|e|  # Network error, 404 Not Found, JSON parse error, etc.
  p e
}

CSRF tokens

You don't need to care about CSRF tokens if the server is a Rails app because Ovto.fetch automatically send X-CSRF-Token header if the page contains a meta tag like <meta name='csrf-token' content='....' />.