Skip to content

Issue while making ajax call with javascript #79

Description

@alaksandarjesus

Hi.

I tried all the below code

    <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="jquery.min.js"></script>
</head>

<body>

  <script src="jquery.min.js"></script>
  <script>
      const url = 'https://putsreq.com/bWVINoAIwR78cl7ajqhX';

      // Using Jquery

      jQuery.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        processData: false,
        data: '{"foo":"bar"}',
        success: function (data) {
          console.error(JSON.stringify(data));
        },
        error: function () {
          console.error("Cannot get data");
        }
      });

      /*
      // Using Vannila JS

      let xhr = new XMLHttpRequest();
      xhr.open("POST", url);

      xhr.setRequestHeader("Content-Type", "application/json");

      xhr.onreadystatechange = function () {
         if (xhr.readyState === 4) {
            console.log(xhr.status);
            console.log(xhr.responseText);
         }};

      let data = '{"login":"my_login","password":"my_password"}';

      xhr.send(data);
      */

      /*
       // Using Fetch

      fetch(url, {
        method: 'POST', // or 'PUT'
        headers: {
          // 'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Type': 'text/plain; charset=utf-8',
          // 'Content-Type': 'application/json',


        },
        body: JSON.stringify(data),
      })
        .then((response) => response.text())
        .then((data) => {
          console.log('Success:', data);
        })
        .catch((error) => {
          console.error('Error:', error);
        });
        */
    
  </script>
</body>

</html>

and i am getting response as

image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions