- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我看了又看,但没有什么能完全触及这个问题。
我正在尝试通过 Chrome 中的 JavaScript* 发送 XMLHttpRequest
。这是我的页面:
<!DOCTYPE html>
<html>
<head>
<title>ROAM</title>
<script>
function post_something() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', "post_test.php", true);
xmlhttp.setRequestHeader('Content-Type', 'text/plain');
xmlhttp.send("This is my text.");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
}
}
</script>
</head>
<body>
<table>
<tr><td><input type="button" value="POST a thingy"
onclick="javascript:post_something()">
</input>
</td></tr>
</table>
</body>
</html>
这是我的 PHP:
<?php
print_r($_POST);
/>
这显示在控制台中:
Array
(
)
如果某个地方能告诉我 XMLHttpRequest.send 到底做了什么,它到底发送了什么,PHP 如何解析它,以及 PHP 期望什么,我可以自己解决这个愚蠢的事情。
*请理解我不想使用表单或 jQuery。我想直接使用 XMLHttpRequest 对象,直到我确切了解它的工作原理以及 PHP 如何接收和解析它。
最佳答案
您应该将您的Content-Type
声明为application/x-www-form-urlencoded
并以key1=value1&key2 的形式制作数据字符串=value2&key3=value3
.
function post_something() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', "post_test.php", true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data = "text=This is my text.&text2=This is my second text.";
xmlhttp.send(encodeURI(data));
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
}
}
输出:
Array
(
[text] => This is my text.
[text2] => This is my second text.
)
关于php - 我正在努力做,_POST 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31257444/
我正在尝试为我的项目创建一些单元测试,经过大量挖掘之后,我发现了Effort,这个想法很棒,它模拟数据库而不是处理伪造的DBContext,顺便说一句,很难做到正确使用复杂的架构。 但是,我将用户的电
我正在尝试对某些响应 Entity Framework 数据库上下文的类进行单元测试。为了寻求帮助,我设法找到了一个名为 Effort 的库,它似乎有点旧,而且没有很好的文档记录,但它似乎可以工作,而
这两天,Auto-GPT 爆火 https://github.com/Torantulino/Auto-GPT 它是一款让最强语言模型GPT-4能够自主完成任务的模型,让整个AI圈疯
为什么会出现这个异常?这是错误吗? 我正在使用 EF 测试库 Effort 创建我的数据库的内存实例并遇到这个有趣的场景: 打开DbContext1 将项目添加到表(不保存) 关闭DbContext1
我是一名优秀的程序员,十分优秀!