0

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

MYARCH
  • 1
  • 4
  • Is there anything stopping you from using [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)? IE 11 is the only thing lacking support. It'll make your life a million times easier if you don't need to support IE 11. If you do need IE support, though... not so much. – matthew-e-brown Nov 21 '19 at 19:02
  • Possible duplicate of [Send POST data using XMLHttpRequest](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) – matthew-e-brown Nov 21 '19 at 19:04
  • no it's not a duplicate – MYARCH Nov 23 '19 at 18:35
  • No solution after 2 Week – MYARCH Nov 28 '19 at 12:45
  • @MYARCH What is your files architecture ? Are the HTML and JS files in the same folder ? – Khalenn Nov 28 '19 at 13:00
  • html and js not in the same folder i need to call js file from external link – MYARCH Nov 28 '19 at 13:17
  • @MYARCH So do you update the url variable in the JS consequently ? – Khalenn Nov 28 '19 at 13:19
  • i call the file.js in html using ´´´ ´´´ and the file.js content the xmlhttprequest() function i note that the error came in line http.open – MYARCH Nov 28 '19 at 13:26
  • @MYARCH Are the JS and PHP files in the same folder ? If not, you have to give the correct path in the url variable. Like for example `var url = '..\\server.php';` – Khalenn Nov 28 '19 at 13:40
  • the path is correct i note that xmlhttprequest treat like undefined with file.js when its out of html body – MYARCH Nov 28 '19 at 13:44
  • @MYARCH Do you have an error in the console ? – Khalenn Nov 28 '19 at 13:47

1 Answers1

0

It doesn't matter whether you put the javascript code directly in the HTML page or add it to the script using the src attribute on the script. The problem is, you aren't setting a request header with setRequestHeader(). Add the following line of code just after http.open(), but before http.send() and it'll definitely work:

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

The application/x-www-form-urlencoded format provides a way to encode name-value pairs.

Plabon Dutta
  • 4,101
  • 3
  • 22
  • 29
  • Sorry still not work i add `http.open('POST', url, true); //----------> always stop her http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params);` – MYARCH Nov 29 '19 at 01:15
  • I've taken your code and ran it on my machine after adding `http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');`. And it works fine. I added the `onreadystatechange` code and it alerts with `username :use`. – Plabon Dutta Nov 29 '19 at 12:13
  • try to run it in https://playcode.io/online-javascript-editor try running code it failed to send to php it look like `http.open('POST', url, true //not defined when js file is out of html body ` – MYARCH Nov 29 '19 at 22:21
  • I don't know how you're running php on playcode.io. – Plabon Dutta Nov 29 '19 at 23:25
  • this my php code ` ` – MYARCH Nov 30 '19 at 02:54
  • It runs for me. Please share the link of your playcode.io source code. – Plabon Dutta Nov 30 '19 at 05:18
  • it not send data and not receive answer from server `alert(http.responseText);` you can create a php file and upload it in a server and run the file.js with playcode.io online compiler it run but failed to send data and receive answer – MYARCH Nov 30 '19 at 12:02
  • any suggestion ?? Plabon Dutta I note that the code when it putted directly in html body it work perfectly but when i put the js file like this ` ` it stop to execute js file in ` http.open('POST', url, true); ` i detect this fails because i put a alert message after every line of js file – MYARCH Dec 02 '19 at 18:48
  • Plabon Dutta I upload php on webhost i note that i try to inject the js file using xss injection like this <> and i think that cause the fail of XMLHttpRequest – MYARCH Jan 29 '20 at 18:09