gpt4 book ai didi

node.js - Webduino 从 curl 而不是从 Node 接收 POST 参数

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:27 24 4
gpt4 key购买 nike

这是在 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,顶部是我的):

Requestbin

有什么建议吗?

最佳答案

最后,我将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com