{"id":2024,"date":"2022-04-20T12:17:37","date_gmt":"2022-04-20T03:17:37","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=2024"},"modified":"2022-05-09T05:35:09","modified_gmt":"2022-05-08T20:35:09","slug":"2024","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=2024","title":{"rendered":"<small>Coding style, \u623b\u308a\u5024\u306e\u6e21\u3057\u65b9\u3068\u3001fetch, XMLHttpRequest\u3067\u306erequest <\/small>"},"content":{"rendered":"<p>\uff11\uff0eCoding style<br \/>\n\uff08\uff11\uff09nodejs:  express \u3067\u306e\u623b\u308a\u5024\u306e\u6e21\u3057\u65b9<br \/>\n\uff08\uff12\uff09fetch, XMLHttpRequest\u3067\u306erequest<\/p>\n<p>\uff12\uff0eexprss\u3067\u306e\u623b\u308a\u5024\u306e\u6e21\u3057\u65b9<\/p>\n<pre>\r\nrouter.post('\/send', async function(req, res) {\r\n     const {client_host} = req.body;\r\n\r\n    let code, err;\r\n    [ code, err ] = await to_client.connect(client_host);\r\n\r\n    if(code == 500) {\r\n        let rt = JSON.stringify({\"error\":\"ECONNRESET\"});\r\n        return res.status(400).end(rt);    \r\n    }\r\n    if(code !== 200) {\r\n        let rt = JSON.stringify({\"error\":\"\"});\r\n        return res.status(400).end(err);    \r\n    \r\n    res.json({\r\n        err : 0,\r\n        msg : status.document_uuid\r\n    });\r\n}\r\n<\/pre>\n<p>\uff13\uff0erequest<br \/>\n(1) fetch\u3000<\/p>\n<pre>\r\nfunction evt_send(document_uuid) {\r\n\r\n        const params = {\r\n            document_uuid : document_uuid\r\n        };\r\n\r\n        const url = '\/api\/invoice\/send';\r\n\r\n        const opts = {\r\n            method: 'POST',\r\n            cache: 'no-cache',\r\n            credentials: 'same-origin',\r\n            headers: {\r\n                'Content-Type': 'application\/json'\r\n            },\r\n            body: JSON.stringify(params)\r\n        };\r\n\r\n        let response;\r\n        try {\r\n            response = await fetch( url, opts);\r\n        } catch(err) {\r\n            throw err;\r\n        }\r\n\r\n        let res;\r\n        try {\r\n            res = await response.json();\r\n        } catch(err) {\r\n            throw err;\r\n        }\r\n\r\n        if(response.status == 200) {\r\n            this.API.refresh();\r\n        } ekse {\r\n            alert(\"\u9001\u4fe1\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f.\\n\"+res.err);\r\n        }\r\n}\r\n<\/pre>\n<p>(2) XMLHttpRequest\u3000<\/p>\n<pre>\r\nfunction evt_send(document_uuid) {\r\n\r\n        const params = {\r\n            document_uuid : document_uuid\r\n        };\r\n\r\n        const ajax = new XMLHttpRequest();\r\n        ajax.open('POST', '\/api\/invoice\/send');\r\n        ajax.setRequestHeader('Content-Type', 'application\/json');\r\n        ajax.responseType = \"json\";\r\n        ajax.send(JSON.stringify(params));\r\n\r\n        ajax.onload = () => {\r\n\r\n            const res = ajax.response;\r\n\r\n            if(ajax.status == 200) {\r\n                this.API.refresh();\r\n            } else {\r\n                alert(\"\u9001\u4fe1\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f.\\n\"+res.err);\r\n            }\r\n        }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uff11\uff0eCoding style \uff08\uff11\uff09nodejs: express \u3067\u306e\u623b\u308a\u5024\u306e\u6e21\u3057\u65b9 \uff08\uff12\uff09fetch, XMLHttpRequest\u3067\u306erequest \uff12\uff0eexprss\u3067\u306e\u623b\u308a\u5024\u306e\u6e21\u3057\u65b9 router.post(&#8216;\/&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2024"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2024"}],"version-history":[{"count":18,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2024\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2024\/revisions\/2026"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}