简体   繁体   English

AJAX响应文本为空

[英]AJAX Response Text Empty

I'm creating a system that interacts with Minecraft server using AJAX. 我正在创建一个使用AJAX与Minecraft服务器交互的系统。 My JavaScript code is as follows: 我的JavaScript代码如下:

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代码如下:

<?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 (ie the DIV with the id 'response' remains empty after the query is complete). AJAX查询执行得很好,除了responseText变量似乎不包含响应文本(即查询完成后具有id'响应'的DIV仍然为空)。

The correct contents of the response DIV should be: 响应DIV的正确内容应该是:

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 [20:14:21.855] NS_ERROR_FAILURE:失败@ http://omnicraft.duncan.usr.sh/omnimessage/:154

Line 154 is: xmlhttp.send(); 第154行是: xmlhttp.send(); (or maybe document.getElementById("response").innerHTML=xmlhttp.responseText; (或者可能是document.getElementById("response").innerHTML=xmlhttp.responseText;

Try this 尝试这个
You haven't handled onreadystatechange 你还没有处理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;
    }
  }
}

Try removing the header call in your PHP code. 尝试删除PHP代码中的标头调用。 I'm having a very similar issue that only goes away when I no longer issue a Content-Type header through PHP. 我有一个非常类似的问题,只有当我不再通过PHP发布Content-Type头时才会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM