Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save localhostdotdev/3c3f9291c0703c9bf9d436da9679d755 to your computer and use it in GitHub Desktop.
Save localhostdotdev/3c3f9291c0703c9bf9d436da9679d755 to your computer and use it in GitHub Desktop.

Revisions

  1. localhostdotdev revised this gist May 12, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions post fetch rails json with csrf token.coffee
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    post = (url, options = {}) ->
    post = (url, body) ->
    token = document.querySelector('meta[name="csrf-token"]').attributes.content.value

    fetch("#{url}.json",
    @@ -11,5 +11,5 @@ post = (url, options = {}) ->
    "X-CSRF-Token": token
    redirect: "follow"
    referrer: "no-referrer"
    body: options.body
    body: body
    ).then (response) -> response.json()
  2. localhostdotdev revised this gist May 12, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions post fetch rails json with csrf token.coffee
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,5 @@ post = (url, options = {}) ->
    "X-CSRF-Token": token
    redirect: "follow"
    referrer: "no-referrer"
    body: options.body
    ).then (response) -> response.json()
  3. localhostdotdev created this gist May 12, 2019.
    14 changes: 14 additions & 0 deletions post fetch rails json with csrf token.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    post = (url, options = {}) ->
    token = document.querySelector('meta[name="csrf-token"]').attributes.content.value

    fetch("#{url}.json",
    method: "POST"
    mode: "cors"
    cache: "no-cache"
    credentials: "same-origin"
    headers:
    "Content-Type": "application/json"
    "X-CSRF-Token": token
    redirect: "follow"
    referrer: "no-referrer"
    ).then (response) -> response.json()