Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created May 31, 2018 22:50
Show Gist options
  • Save chermehdi/57d9558b2f5edb80898ed304f8b8fa22 to your computer and use it in GitHub Desktop.
Save chermehdi/57d9558b2f5edb80898ed304f8b8fa22 to your computer and use it in GitHub Desktop.
// suppose this is the array we want to send
let arr = [
{
key: 'key1',
value: 'value1'
},
{
key: 'key2',
value: 'value3'
}
]
// client code
let str = {arr}
function sendWithAjax(url, method, data) {
return new Promise((res, rej) => {
let xhr = new XMLHttpRequest()
if (xhr.readyState === 4) {
if (xhr.status === 200) {
res(xhr.response)
} else {
rej(xhr)
}
}
xhr.open(url, method, true)
xhr.send(data)
})
}
sendWithAjax('/create', 'POST', JSON.stringify(str))
.then(res => {
// success here
})
// server code
app.post('/create', (req, res) => {
let obj = req.body
console.log('object is ', obj)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment