Created
May 31, 2018 22:50
-
-
Save chermehdi/57d9558b2f5edb80898ed304f8b8fa22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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