我正在从事一个嵌入式STM32 httpd项目。我有部分项目使用GET方法处理html代码。我能够使用该方法完成需要运行的任务。
现在,我需要输入用户名和密码才能访问GET页面。我想使用POST方法,因此UN和PW不会显示在URL栏中。我有用于将用户名和密码提交到服务器的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<form action="" method="post">
<p>
<input type="text" id="txtUsername" name="username" placeholder="Enter username" require/>
</p>
<p>
<input type="password" id="txtPassword" name="password" placeholder="Enter Password" required/>
</p>
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
我在服务器代码中设置了断点,服务器接收到UN和PW。我可以将UN和PW与服务器上存储的凭据进行比较,以查看它们是否匹配。如果它们匹配,我希望能够将“好”发送回浏览器,如果不匹配,则发送“不好”。我可以使用此代码,但不知道如何将响应返回给浏览器。
我花了一些时间在线阅读有关javascript,ajax和xhttp的信息。我已经弄清楚我需要使用脚本来捕获来自服务器的响应。除了非常基础之外,我没有HTML经验。我从w3schools.com找到了这个示例。
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "demo_post2.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
}
</script>
</body>
</html>
当我在w3schools网站上运行示例代码时,它返回“ Hello Henry Ford”。当我在设置了断点的项目板上运行它时,在服务器的POST部分中看到“ fname = Henry”和“ lastname = Ford”。但显然我没有任何回应。我认为他们网站上的响应来自他们服务器上运行的“ demo_post2.asp”程序。
STM32 httpd服务器仅支持javacript,ajax和xml。我确实激活了CGI和SSI并正在为GET程序工作。如果POST的响应为“良好”,我想将浏览器重定向到服务器上的另一个页面。如果响应为“错误”,我想显示一条消息“用户名或密码错误”,并保留在同一登录页面上。
有人可以引导我指向说明如何将数据返回到浏览器窗口的链接或文档吗?
luek,我确实正在运行SSI,可以使用它。我认为发布完成后,服务器可能会将char“ * response_uri”返回给浏览器。我不确定,但是由于名称的原因,这似乎很有意义。
Antoni,这是服务器上的代码。这只是普通的C语言代码。
err_t httpd_post_receive_data(void *connection, struct pbuf *p)
{
// payload comes in as a string "username=username&password=password"
char *ret, readval[48];
char un[8], pw[24]; // pw is size 24 to allow for several special characters
char delim[2];
uint8_t x = 0, y = 0, pos;
strcpy(delim, "=");
strcpy(readval, p->payload);
ret = strstr(readval, delim);
pos = ret - readval;
pos++; // step past = character to username
for(x = pos; x < 25; x++) // 25 is 9(x start) + 16(un size)
{
if(readval[x] == 0x26) // 0x26 is & character
break;
un[y] = readval[x];
y++;
}
un[y] = '\0'; // add string terminate character
x++; // step past & character
pos = x + 9; // 9 is length of word "password" plus \0
for(x = (pos-9); x < pos; x++)
{
if(readval[x] == 0x3D) // 0x3D is = character
break;
}
y = 0;
x++; // step past = character
pos = x + 24; // 24 is size of pw
for(x = (pos-24); x < pos; x++)
{
if(readval[x] == '\0')
break;
pw[y] = readval[x];
y++;
}
pw[y] = '\0'; // add string terminate character
// add code here to compare username and password to values stored
// in EEPROM. For testing purposes, just copy "good" to postval
strcpy(postval, "good"); // postval is a global variable
pbuf_free(p);
return ERR_OK;
}
void httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len)
{
response_uri_len = strlen(postval); // postval is a global variable
snprintf(response_uri, response_uri_len, postval); // return un and pw check result
connection = NULL;
asm("NOP");
}