# Fetch

### Fetch - Method Get (recevoir)

Voici un exemple simple : après avoir vérifié la requète, on récupère la propriété `greetings` de l'objet `queryString`, pour l'afficher dans une div `#hello-result` lorsque l'on clique sur un bouton `#ask-hello`

```javascript
const helloResult = document.getElementById("hello-result");
const askHelloButton = document.getElementById("ask-hello");

function askHello(){
  fetch("https://mockbin.com/request?greetings=salut")
  .then(function(res) {
    if (res.ok) {
      return res.json();
    }
  })
  .then(function(value) {
    // c'est ici que l'on récupère la valeur de la propriété "greetings" de l'objet "queryString"
    // console.log(value.queryString.greetings);
    helloResult.innerHTML = value.queryString.greetings;
  })
  .catch(function(err) {
    // Une erreur est survenue
  });
}

askHelloButton.addEventListener('click', event => {
  askHello();
});



```

### Fetch - Method Post (envoyer)

Dans cet exemple, on cherche à envoyer la donnée d'un input vers une API, et à afficher le contenu de la donnée au `submit`, sans recharger la page (`preventDefault();`).

<pre class="language-javascript"><code class="lang-javascript">function send(e){
  e.preventDefault();
  fetch("https://mockbin.com/request", {
	method: "POST",
	headers: { 
          'Accept': 'application/json', 
          'Content-Type': 'application/json' 
<strong>        },
</strong>	body: JSON.stringify({value: document.getElementById("value").value})
        })
        .then(function(res) {
          if (res.ok) {
            return res.json();
          }
        })
        .then(function(value) {
            document.getElementById("result").innerText = value.postData.text;
        });
}

document.getElementById("form").addEventListener("submit", send);
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cyrille.dev/javascript/fetch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
