xml使用xmlHttp.open处理多个变量

xml handling multiple variables with xmlHttp.open

本文关键字:变量 处理 open 使用 xmlHttp xml      更新时间:2023-09-26

当发送到服务器时,我无法回显两个变量 xmlHttp.open .我知道我需要使用 & 来分隔变量,但我收到服务器响应错误并且没有输出。如果只发送foo变量,则代码工作正常。我认为这一定是一个简单的错误,我只需要另一双眼睛。

Javascript 文件:

var foo = "foo" 
var bar = "bar"
xmlHttp.open("GET", "update.php?foo=" + foo+ "&bar=" + bar, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);

更新.php:

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
    $foo= $_GET['foo'];
    $bar= $_GET['bar'];
    echo 'Variable foo: ' . $foo. ' Variable bar ' . $bar; // Server response error and no output
echo '</response>';
?>

您是否实例化了 xmlHttp 对象。

var foo = "foo",
  bar = "bar",
  xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "update.php?foo=" + foo + "&bar=" + bar, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);

或者,我建议使用JQuery来解决IE和其他人使用不同对象结构的事实。

$.get('/update.php', {foo:foo,bar:bar}, function(result) {
  console.log(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>