Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while making ajax call with javascript #79

Open
alaksandarjesus opened this issue Feb 22, 2023 · 1 comment
Open

Issue while making ajax call with javascript #79

alaksandarjesus opened this issue Feb 22, 2023 · 1 comment

Comments

@alaksandarjesus
Copy link

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

@phstc
Copy link

phstc commented Feb 26, 2023

@alaksandarjesus I've made some changes to prevent this from happening. Can you try it again?

There's something very confusing going on with these requests. With cURL, the body is received as-is, but for some reason, with the methods above, an addition \" was wrapping the body, and causing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants