作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是在 Arduino 上运行的代码,使用 Webduino图书馆:
void handleConfigRequest(WebServer &server, WebServer::ConnectionType type, char *, bool) {
// POST request to receive new config
if (type == WebServer::POST) {
bool repeat;
char name[16], value[50];
do {
// Returns false when no more params to read from the input
repeat = server.readPOSTparam(name, 16, value, 50);
if (strcmp(name, "dName") == 0) {
strcpy(deviceName, value);
Serial.println(value);
}
} while (repeat);
}
当在命令行上从 curl 执行以下命令时,这按预期工作(并通过串行打印“测试”):
curl http://10.0.1.141/config -d "dName=Test"
我还测试了一个简单的 HTML 表单,它也可以在提交时通过串行打印“测试”:
<form action="http://10.0.1.141/config">
<input type="text" name="dName" value="test">
<input type="submit" value="Send">
</form>
但是,以下 Node.js 使用 request代码不起作用:
var options = {
url: "http://10.0.1.141/config",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "*/*",
"User-Agent": "TestingScript"
},
form: {dName: "Test"}
}
request.post(options, function(error, response, body) {
console.log(error, response, body)
})
使用 Node 代码,Arduino 确认 HTTP POST 请求(在本例中,通过闪烁 LED),但不会将“测试”打印到串行。
我已将 curl 和我的 Node 代码指向 requestb.in 页面,您可以看到请求本身看起来是相同的(底部的是 curl,顶部是我的):
有什么建议吗?
最佳答案
最后,我将 Node request
代码替换为以下 jQuery:
$.ajax({
type: "POST",
url: "http://10.0.1.141/config",
data: {dName: "Test"}
}, function(data( {
console.log(data)
}
效果很好。奇怪的。
关于node.js - Webduino 从 curl 而不是从 Node 接收 POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43873339/
这是在 Arduino 上运行的代码,使用 Webduino图书馆: void handleConfigRequest(WebServer &server, WebServer::Connection
我是一名优秀的程序员,十分优秀!