AJAX请求使用PHP
AJAX request using PHP
我想在点击按钮时触发AJAX请求,但我无法在后端触发它。
index.php:
<html>
<head>
<script type="text/javascript">
var req = new XMLHttpRequest();
function send1()
{
req.open("GET", "process.php?q=hello", true);
req.send();
alert(req.responseText);
}
</script>
</head>
<button onclick=send1()>send</button>
</html>
process.php:
<?php
$new= $_GET['q'];
echo $new;
?>
这应该会在警报框中给我"你好",为什么不是呢?
AJAX中的第一个A表示"异步"。你需要做的是倾听readyState的变化:
req.open(...);
req.onreadystatechange = function() {
if( this.readyState == 4) {
if( this.status == 200) alert(this.responseText);
else alert("HTTP error "+this.status+" "+this.statusText);
}
};
req.send();
相关文章:
- Ajax POST请求没有'我不了解PHP
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 通过XHR请求将MYSQL数组从PHP返回到Javascript
- 在PHP中检测AJAX请求来自哪个文件
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- 如何根据传入的哈希标签发送PHP请求
- 在同一主机上对PHP文件使用XMLHTTP请求的Javascript违反了同源策略
- php请求带有多个对象json-jquery
- 处理来自同一页面上ajax驱动的Fancybox元素的PHP请求
- PHP 请求导致 AJAX 重新加载错误的页面
- 对php请求的post-jquery请求使用一个delete函数
- AJAX-PHP请求本地页面安全
- 通过AJAX的PHP请求失败
- Ajax和PHP请求在x次尝试后被取消
- 通过ajax发送php请求
- PHP请求的多项选择列表
- AJAX post到php请求问题
- 如何在Angular JS中从PHP请求中获取数据
- 避免同时发生两个php请求
- 多个PHP请求导致页面崩溃