【发布时间】:2012-04-07 04:55:23
【问题描述】:
您好,我想将数据发送到服务器,然后让 php 文件保存它。 但目前我什至连一个简单的 POST 都无法工作。
这是 javascript 和 html 部分: 该函数在头部声明:
function sendTableToServer(){
var xhttp;
if (window.XMLHttpRequest){
xhttp=new XMLHttpRequest();
} else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("POST","http://music.collwyncraig.info/hajimama/save_setlist.php",true);
xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhttp.send("fsong=AbandonSeoul");
}
那么这个html就在body里面:
<button type="button" onclick="sendTableToServer()">Save Setlist</button>
这是php文件
<?php
if (isset($_POST["fsong"])){
echo 'trying';
$song = $_POST["fsong"];
echo $song;
echo 'ok';
}
?>
当我点击按钮时,什么也没有发生。
如果我使用<form> 和提交按钮,浏览器会加载 php 文件,我可以访问输入。但是,我想使用这样的函数,因为我要发送的信息将使用 javascript 和 HTML DOM 从 html 页面中取出。
那么,为什么什么都没有发生呢? :)
【问题讨论】:
-
XHTTP 回调在哪里?
-
有没有想过使用jQuery?
标签: javascript ajax function post