--- id: 587d7faf367417b2b2512be9 title: JavaScript の XMLHttpRequest メソッドでデータをポストする challengeType: 6 forumTopicId: 301504 dashedName: post-data-with-the-javascript-xmlhttprequest-method --- # --description-- 以前の例では、外部リソースからデータを受け取りました。 そのリソースが AJAX リクエストをサポートし、あなたが URL を知っていれば、外部リソースにデータを送信することもできます。 JavaScriptの `XMLHttpRequest` メソッドは、データをサーバーにポストするためにも使用されます。 次に例を示します。 ```js const xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 201){ const serverResponse = JSON.parse(xhr.response); document.getElementsByClassName('message')[0].textContent = serverResponse.userName + serverResponse.suffix; } }; const body = JSON.stringify({ userName: userName, suffix: ' loves cats!' }); xhr.send(body); ``` これらのメソッドのいくつかは見覚えがあるでしょう。 Here the `open` method initializes the request as a `POST` to the given URL of the external resource, and passes `true` as the third parameter - indicating to perform the operation asynchronously. The `setRequestHeader` method sets the value of an HTTP request header, which contains information about the sender and the request. It must be called after the `open` method, but before the `send` method. The two parameters are the name of the header and the value to set as the body of that header. Next, the `onreadystatechange` event listener handles a change in the state of the request. A `readyState` of `4` means the operation is complete, and a `status` of `201` means it was a successful request. Therefore, the document's HTML can be updated. Finally, the `send` method sends the request with the `body` value. The `body` consists of a `userName` and a `suffix` key. # --instructions-- Update the code so it makes a `POST` request to the API endpoint. Then type your name in the input field and click `Send Message`. Your AJAX function should replace `Reply from Server will be here.` with data from the server. Format the response to display your name appended with the text `loves cats`. # --hints-- Your code should create a new `XMLHttpRequest`. ```js assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g)); ``` Your code should use the `open` method to initialize a `POST` request to the server. ```js assert(code.match(/\.open\(\s*?('|")POST\1\s*?,\s*?url\s*?,\s*?true\s*?\)/g)); ``` Your code should use the `setRequestHeader` method. ```js assert( code.match( /\.setRequestHeader\(\s*?('|")Content-Type\1\s*?,\s*?('|")application\/json;\s*charset=UTF-8\2\s*?\)/g ) ); ``` Your code should have an `onreadystatechange` event handler set to a function. ```js assert(code.match(/\.onreadystatechange\s*?=/g)); ``` Your code should get the element with class `message` and change its `textContent` to `userName loves cats` ```js assert( code.match( /document\.getElementsByClassName\(\s*?('|")message\1\s*?\)\[0\]\.textContent\s*?=\s*?.+?\.userName\s*?\+\s*?.+?\.suffix/g ) ); ``` Your code should use the `send` method. ```js assert(code.match(/\.send\(\s*?body\s*?\)/g)); ``` # --seed-- ## --seed-contents-- ```html

Cat Friends

Reply from Server will be here

``` # --solutions-- ```html

Cat Friends

Reply from Server will be here

```