How to send post request json to javascript?

  • 0
    How to send a post request json to javascript, and get it to php?
    Sending data not through a form, but I want to by condition, for example if () {}
    Needed in pure js, no jquery
    Thank you in advance
    JavaScript Anonymous, Sep 18, 2019

  • 1 Answers
  • 0
    const url = 'https://example.com/profile';
    const data = { username: 'example' };

    try {
    const response = await fetch(url, {
    method: 'POST', // или 'PUT'
    body: JSON.stringify(data), // данные могут быть 'строкой' или {объектом}!
    headers: {
    'Content-Type': 'application/json'
    }
    });
    const json = await response.json();
    console.log('Успех:', JSON.stringify(json));
    } catch (error) {
    console.error('Ошибка:', error);
    }


    https://developer.mozilla.org/en/docs/Web/API / Fetc ...
    Sean Meyer

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!