发送带有JavaScript的HTTP请求
Send a HTTP Request w/ JavaScript
所以我有一个类似的JavaScript函数,
var ajax = function(data, url, method, onfinish){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
onfinish(xmlhttp.responseText);
}
};
xmlhttp.open(method, "pages/page.php?cv=1", true);
xmlhttp.send("cv=1");
};
我有一个空链接,应该运行功能
<a href="javascript:void(0)" onclick="ajax('cv=1', 'pages/page.php', 'POST', set)" class="link">Posts</a>
这是我的php文件,
<?php
$cv = $_POST["cv"];
if ($cv == "p1") {
include("posts.php");
} else if ($cv == "p2") {
include("users.php");
} else if ($cv == "p3") {
include("write.php");
} else if ($cv == "p4") {
include("signup.php");
}
?>
但我一直收到这个错误,
注意:未定义的索引:cv-in/home/cabox/workspace/pages/page.php第2行
如果要使用POST
,则需要添加:
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
所以你的功能是:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(method, url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
}
}
xmlhttp.send(data);
然后在PHP中:
if(isset($_POST['cv'])) {
$cv = $_POST["cv"];
if ($cv == "1") {
include("posts.php");
} else if ($cv == "2") {
include("users.php");
} else if ($cv == "3") {
include("write.php");
} else if ($cv == "4") {
include("signup.php");
}
}
您可以这样做:
$cv = isset($_POST['cv']) ? $_POST['cv'] : '';
我希望能帮助你!
相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- Firefox插件-如何发出http请求
- 验收测试主要针对传出的http请求
- 验证HTTP请求来自NFC标签上的点击
- 如何制作'http请求'在Node js中同步
- 为什么我在Cloud9中的http请求总是返回错误
- AngularJS Typeahead和$http请求限制
- HTTP请求使用cURL,但在浏览器中使用javascript时会得到CORS响应
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- 将http请求中的数据传递给C#控制器
- 在初始化路由之前发送$http请求
- 从客户端捕获 HTTP 请求(包括 ajax)