0

I'm experimenting a bit with jQuery

<html>
<head>
<title>post test</title>

<script src="jquery-3.2.1.min.js"></script>

<script>

$.post('http://lab.anta40.net/dummy/login.php',
      { 
        'username': 'John Doe', 
        'password': 'hello123'
      },

      function (response, status) {
        alert(response.status);
      }
    );

</script>
</head>

<body>
test...
</body>
</html>

My intention is the page will display an alert box which contains the response of the POST request. Running the code on Chrome 61 doesn't yield anything. No alert box at all. What is wrong here?

anta40
  • 6,511
  • 7
  • 46
  • 73
  • You should catch the error callback and see what went wrong – AshBringer Oct 02 '17 at 10:49
  • Your code itself is working fine: http://jsfiddle.net/31nx930a/. However if you check the console you'll see this error: `No 'Access-Control-Allow-Origin' header is present on the requested resource`. This means the third party domain does not return CORS headers in the response, hence JS cannot make a request to it due to the Same Origin Policy. – Rory McCrossan Oct 02 '17 at 10:49
  • @RoryMcCrossan Hmm... so what I have to do to fix it? – anta40 Oct 04 '17 at 11:07
  • You can see some solutions in the duplicate I marked. Long story short, make the request from the server, not from JS – Rory McCrossan Oct 04 '17 at 11:09

0 Answers0