123456789101112131415161718192021222324252627282930313233343536373839404142 |
- //Closures and scopes
- async function jsonPost(url, data) {
- let response = await fetch(url, {
- method: "POST",
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- });
- if (response.ok) {
- console.log('work')
- let json = await response.json();
- console.log(json)
- return json;
- } else {
- new Error('jsonPost failed'+ response.status);
- }
- }
- /* function jsonPost(url, data) {
- return new Promise((resolve, reject) => {
- var x = new XMLHttpRequest();
- x.onerror = () => reject(new Error('jsonPost failed'))
- //x.setRequestHeader('Content-Type', 'application/json');
- x.open("POST", url, true);
- x.send(JSON.stringify(data))
- x.onreadystatechange = () => {
- if (x.readyState == XMLHttpRequest.DONE && x.status == 200) {
- resolve(JSON.parse(x.responseText))
- } else if (x.status != 200) {
- reject(new Error('status is not 200'))
- }
- }
- })
- } */
- jsonPost('https://jsonplaceholder.typicode.com/posts', {func: 'addMessage', nick: "HellWords", message: 'AHHAAAH'}).then(res => console.log(res)
- )
|