gpt4 book ai didi

javascript - AJAX 响应文本为空

转载 作者:行者123 更新时间:2023-11-30 12:54:04 25 4
gpt4 key购买 nike

我正在创建一个使用 AJAX 与 Minecraft 服务器交互的系统。我的 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;
}

我的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;
}

?>

我的表单如下:

<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>

AJAX 查询执行得很好,除了 responseText 变量似乎不包含响应文本这一事实(即,在查询完成后,id 为“response”的 DIV 仍然为空)。

响应DIV的正确内容应该是:

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

然而,事实并非如此。我做错了什么?

编辑:更多信息:

我收到以下错误:

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

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

最佳答案

试试这个
你还没有处理 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;
}
}
}

关于javascript - AJAX 响应文本为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com