Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Why xmlhttprequest response is in html code

For some reason when I am sending an http request using xmlhttp the answer that I'm getting is in a html form

function HttpRequest(method, url, username, password) {
    const xhr = new XMLHttpRequest();
    let received_data;
    xhr.onreadystatechange = () => {
        if (xhr.readyState == 4 && xhr.status == 200) {
            console.log(xhr.responsejson);
            if (xhr.responseText == "access granted")
                console.log("ok");
            else
                console.log("error");
        }
    }
    xhr.open(method, url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send('username=' + username + '&password=' + password);
    xhr.onload = function() {
        console.log("connected to " + url);
    }
 }

function send_data() {
    let method = 'POST';
    let url = 'https://localhost:83/android_app/server.php'; //url of the php 
server(must be https:// (secure) in order to make the connection using cordova app)
    let username = document.getElementById('username').value;
    let password = document.getElementById('password').value;
    HttpRequest(method, url, username, password)
   }

also the php server that I am sending this request it just that:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php

header('Access-Control-Allow-Origin: *');

$name = $_POST['username'];
$password = $_POST['password'];
if($name=="admin" && $password=="1234"){
    echo "access granted";
}
else{
    echo "access denit";
}


?>

</body>
</html>

this is the response that I am getting

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can think of a function called via AJAX as if it were a subroutine. It should only return what the caller wants. Currently with all that HTML in there that is being sent back to the AJAX call as well as the access granted and therefore making what you want difficult to find.

So if you change the server.php code to

<?php

header('Access-Control-Allow-Origin: *');

$name = $_POST['username'];
$password = $_POST['password'];
if($name=="admin" && $password=="1234"){
    echo "access granted";
}else{
    echo "access denit";
}

The all you will get returned to the AJAX call is either access granted or access denit, which is what you are testing for in the javascript

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!