use fetch to post data

علی ذوالفقار
1401/10/05 18:48:15 (17)
// post json : 
let payload = { a : 1 , b : 'Textual content' }; 

(async () => {
  const rawResponse = await fetch('URL', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(payload)
  });
  const content = await rawResponse.json();
  console.log(content);
})();


// post formdata : 
(async () => {
    var data = new FormData();
    data.append( "json", JSON.stringify( payload ) );

    const rawResponse = await fetch('URL', {
      method: 'POST',
      headers: {},
      body: data
    });
    const content = await rawResponse.json();
    console.log(content);
  })();
Back