Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mpalmr/96cd777a0f48cc5d6185575cf7a75b6d to your computer and use it in GitHub Desktop.
Save mpalmr/96cd777a0f48cc5d6185575cf7a75b6d to your computer and use it in GitHub Desktop.
<script>
// alert("test");
var req = new XMLHttpRequest();
req.open("POST", "/api/login");
req.send(JSON.stringify({"username":"test", "password":"password123"});
</script>
@mpalmr
Copy link
Author

mpalmr commented Oct 7, 2017

var requestBody = {
  username: 'test',
  password: 'password123',
};

fetch('/api/login', {
  method: 'POST',
  body: JSON.stringify(requestBody),
}).then(functino (response) {
  if (response.ok) return response.json(); // Can use response.text() for non-JSON
  throw new Error('invalid response');
}).then(function (response) {
  document.location = '/new/url/path';
}).catch(function (ex) {
  console.error(ex);
  document.location = '/new/url/path?error=true';
});

@benburkhart1
Copy link

benburkhart1 commented Oct 7, 2017

you misspelled function @mpalmr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment