put the javascript code directly in the HTML page, it does work. Howcome it does not work if I reference the script using the src attribute on the script This is my HTML page:
<html>
<head></head>
<body>
<script src="file.js"></script>
</body>
</html>
I can not get the following javascript code to send data to a PHP page: this is inside file.js
var http = new XMLHttpRequest();
var url = 'server.php';
var params = 'a=use&b=12345678';
http.open('POST', url, true); // error javascript stop always here
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200)
{ alert(http.responseText); } }
http.send(params);
and this is inside server.php
:
<?php
$username = $_POST['a'];
$password = $_POST['b'];
echo 'username :' . $username;
?>
When I put the javascript code directly in the HTML page, it does work. Howcome it does not work if I reference the script using the src
attribute on the script
tag?
for example when i put the file.js script in online compiler like 'playcode.io' it does not work