Javascript AJAX Post成功,但PHP没有响应
我在AJAX POST-to-PHP调用中遇到问题 PHP文件(tableOutput.PHP)中的JS 试戴软呢帽和窗户。代码位于服务器上。浏览器:Firefox Ajax发布成功。我收到一个保存“SAVED!”的警告框,但页面上没有任何回音。如果改为使用window.location.href,则getInfo.php会回显到该页面。问题是我被重定向到getInfo.php页面,这是我不想要的Javascript AJAX Post成功,但PHP没有响应,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我在AJAX POST-to-PHP调用中遇到问题 PHP文件(tableOutput.PHP)中的JS 试戴软呢帽和窗户。代码位于服务器上。浏览器:Firefox Ajax发布成功。我收到一个保存“SAVED!”的警告框,但页面上没有任何回音。如果改为使用window.location.href,则getInfo.php会回显到该页面。问题是我被重定向到getInfo.php页面,这是我不想要的 如何让Ajax发布到getInfo.php文件?为什么不起作用?看起来您正在尝试混合两种不同的机制
如何让Ajax发布到getInfo.php文件?为什么不起作用?看起来您正在尝试混合两种不同的机制(AJAX和post back)。当您使用AJAX时,简单地
echo
-ing输出不会将该内容插入DOM(就像使用完整回发时那样)。您的echo
语句将数据放入输出流,然后由success
函数使用。除非以编程方式(使用javascript/jQuery)将其插入DOM,否则它将一直存在。您可以手动将其插入DOM中。有很多方法可以做到这一点。关键是查看您的响应对象。这是一种可能性:
function saveChanges(){
$.ajax({
url: "getInfo.php/",
type:'POST',
data: {'ipString':changedArr},
success: function(response){
alert("SAVED!");
//add the script element to the DOM body
$(response).appendTo('body');
},
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
console.log(changedArr);
}
重要的是要理解,当包含一个php文件(比如getInfo.php)时,输出是在客户端编写的,php无法再访问它 您需要的是请求getInfo.php,获取它的响应,然后在客户端编写它 客户:
<script>
function saveChanges(){
$.ajax({
url: "getInfo.php/",
type:'POST',
data: {'ipString':changedArr},
success: function(textResponse /* you MUST use this parameter*/){
alert("SAVED!");
// textResponse is the string the server sends. do whatever you want with this
document.getELementById("out").innerHTML = textResponse;
},
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
console.log(changedArr);
}
</script>
<div id="out"></div>
最后,如果您希望灵活,让客户端根据服务器发送的内容进行操作,您应该尝试使用JSON,它可以将对象转换为字符串,反之亦然。问题不在于发布的代码。 在getInfo.php的开头,我忘了添加“php” 它是:
为什么在url:“getInfo.php/”,
中有一个尾随斜杠?在php文件中的if语句外放置一个回音,以确保它正确。很可能没有输入if语句。@ChrisForrence,我不记得为什么会有斜杠。我删除了它。@Daniel,在使用tableOut.php的if语句之外有单独的回音,也有单独的回音。当我添加“echo$_REQUEST['ipString'];”时,页面会打印出“注意:第113行/getInfo.php中的未定义索引:ipString”,即使在我点击保存按钮之后也是如此。谢谢你刚才的建议。似乎没有什么不同的事情发生。我可能错过了什么。
<div class=form>
<form onsubmit="fillTable()" >
<input type="submit" name="deny" value="Denied" />
<input type="submit" name="permit" value="Permitted" />
</form>
</div>
$test="'CREATE'";
if( isset( $_GET['deny'] )){
$test="'DENY'";
}
if( isset( $_GET['permit'] )){
$test="'CREATE'";
}
function saveChanges(){
$.ajax({
url: "getInfo.php/",
type:'POST',
data: {'ipString':changedArr},
success: function(response){
alert("SAVED!");
//add the script element to the DOM body
$(response).appendTo('body');
},
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
console.log(changedArr);
}
<script>
function saveChanges(){
$.ajax({
url: "getInfo.php/",
type:'POST',
data: {'ipString':changedArr},
success: function(textResponse /* you MUST use this parameter*/){
alert("SAVED!");
// textResponse is the string the server sends. do whatever you want with this
document.getELementById("out").innerHTML = textResponse;
},
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
console.log(changedArr);
}
</script>
<div id="out"></div>
<?php
if (isset($_POST['ipString'])) {
echo "A request has been made";
}
?>
<?php
if (isset($_POST['ipString'])) {
?>
A request has been made
<?php
}
?>
<?php
if (isset($_POST['ipString'])) {
echo "A request has been made";
exit(); // exit here so ajax doesn't get the rest of the file
}
?>
<html>
<head></head>
<body></body>
</html>
<?
<?php