Igor
Igor

Reputation: 558

AJAX Response Text Empty

I'm creating a system that interacts with Minecraft server using AJAX. My JavaScript code is as follows:

function doMessage(name, message)
    {
        var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","http://duncan.usr.sh/omnicraft/api/broadcast-msg.php?message=" + message + "&user=" + name,false);
xmlhttp.send();
document.getElementById("response").innerHTML=xmlhttp.responseText;
}

My PHP code is as follows:

<?php
header('content-type: text/plain');

echo "OmniCraft/Api/RequestHTTP";

if($_GET['message'] == "" or ctype_space($_GET['message']) or $_GET['user'] == "" or ctype_space($_GET['user']))
{

    echo "\nOmniCraft/Api/Request/Failed";
    echo "\nRequest invalid: No message specified";
    exit;
}

include_once '../MinecraftQuery.class.php';

    $Query = new MinecraftQuery( );

    try
    {
        $Query->Connect( 'localhost', 25565 );

    $info = $Query->GetInfo( );





    }
    catch( MinecraftQueryException $e )
    {
        echo "\nOmniCraft/Api/Request/Failed";
    echo "\nServer not online";
    exit;
    }

include_once("rcon.class.php"); //Include this file
$r = new rcon("127.0.0.1",25575,"notRealPasswordHere"); //create rcon object for server on the rcon port with a specific password
if($r->Auth()) //Connect and attempt to authenticate
{
$message = $_GET['message'];
$user = $_GET['user'];
$r->rconCommand("/tellraw @a {'text':'<$user using OmniMessage> $message','color':'white'}"); //send a command
echo "\nOmniCraft/Api/Request/Success";
echo "\nMessage sent successfully!";
}
else
{
echo "\nOmniCraft/Api/Request/Failed";
echo "\nUnable to authenticate to RCON";
exit;
}

?>

My form is as follows:

<form >
    <div class="input">
    <input type="text" placeholder="Message" style="padding: 20px 28px;font-size: 25px;" required="true" name="message">
    </div>
    <div class="input">
    <input type="text" placeholder="Name" style="padding: 20px 28px;font-size: 25px;" required="true" name="name">
    </div>

    <button class="btn btn-large btn-success" type="button" onclick="doMessage(this.form.name.value, this.form.message.value)">Send</button>
    <div>
    <p class="lead" id="preview">&lt; using OmniMessage&gt;</p>
    <div>
    <div id="response">
    </div>

The AJAX query executes well, except for the fact that the responseText variable seems to not contain the response text (i.e. the DIV with the id 'response' remains empty after the query is complete).

The correct contents of the response DIV should be:

OmniCraft/Api/RequestHTTP
OmniCraft/Api/Request/Success
Message sent successfully!

However, this is not the case. What am I doing wrong?

EDIT: More Info:

I get the following error:

[20:14:21.855] NS_ERROR_FAILURE: Failure @ http://omnicraft.duncan.usr.sh/omnimessage/:154

Line 154 is: xmlhttp.send(); (or maybe document.getElementById("response").innerHTML=xmlhttp.responseText;

Upvotes: 1

Views: 2267

Answers (2)

AstroPig7
AstroPig7

Reputation: 111

Try removing the header call in your PHP code. I’m having a very similar issue that only goes away when I no longer issue a Content-Type header through PHP.

Upvotes: 0

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11707

Try this
You haven't handled onreadystatechange

function doMessage(name, message) {
var xmlhttp; 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari 
  xmlhttp=new XMLHttpRequest(); 
} else {
// code for IE6, IE5 
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
}
 xmlhttp.open("GET","http://duncan.usr.sh/omnicraft/api/broadcast-msg.php?message=" + message + "&user=" + name,false); 
xmlhttp.send(); 
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("response").innerHTML=xmlhttp.responseText;
    }
  }
}

Upvotes: 3

Related Questions