下面是使用韦迪诺库在Arduino上运行的代码:
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执行以下操作时,这将按预期工作(并在串行上打印"Test“):
卷曲http://10.0.1.141/config -d "dName=Test“
我还测试了一个简单的HTML表单,它在提交时还可以在串行上打印"Test“:
<form action="http://10.0.1.141/config">
<input type="text" name="dName" value="test">
<input type="submit" value="Send">
</form>但是,以下使用Node.js代码的请求不起作用:
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请求(在本例中,通过闪烁LED),但不将"Test“打印为串行。
我将curl和Node代码都指向了一个requestb.in页面,您可以看到请求本身似乎是相同的(下面的是curl,顶部是我的):

有什么建议吗?
发布于 2017-05-11 14:07:44
最后,我将Node request代码替换为以下jQuery:
$.ajax({
type: "POST",
url: "http://10.0.1.141/config",
data: {dName: "Test"}
}, function(data( {
console.log(data)
}效果很好。很奇怪。
https://stackoverflow.com/questions/43873339
复制相似问题