From 542b2d221d31a24d46459d937f163bb2f37cd1d1 Mon Sep 17 00:00:00 2001 From: kyz Date: Sun, 26 Oct 2025 22:40:14 +0800 Subject: [PATCH] modify post data process --- src/user_web_server.c | 34 ++------------------- test.html | 69 ++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/user_web_server.c b/src/user_web_server.c index c728cc5..7037951 100644 --- a/src/user_web_server.c +++ b/src/user_web_server.c @@ -539,24 +539,10 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void // 使用cJSON解析JSON格式的请求体 if( process_http_post_message(pss->url, pss->post_body, pss->post_body_len, &resp) == 0){ // 调试输出,查看解析后的参数 - printf("解析JSON参数: text1='%s', send='%s'\n", text1 ? text1 : "", send ? send : ""); + printf( "process http post msg, resp[%s]\n", resp ); - /* 使用cJSON动态构建JSON响应 */ - cJSON *root = cJSON_CreateObject(); - if (root) { - cJSON_AddStringToObject(root, "method", "POST"); - cJSON_AddStringToObject(root, "status", "success"); - cJSON_AddStringToObject(root, "message", "This is a JSON POST response"); - - // 创建data对象 - cJSON *data = cJSON_CreateObject(); - if (data) { - cJSON_AddStringToObject(data, "text1", text1 ? text1 : ""); - cJSON_AddStringToObject(data, "send", send ? send : ""); - cJSON_AddItemToObject(root, "data", data); - } - char *json_body = cJSON_PrintUnformatted(root); + char *json_body = resp; json_len = json_body ? strlen(json_body) : 0; /* 重置缓冲区指针用于HTTP头 */ @@ -566,17 +552,11 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK, "application/json", json_len, &p, end) < 0) { printf("设置HTTP头失败\n"); - free(text1); - free(send); - cJSON_Delete(root); - if (json_body) free(json_body); + if (json_body) free(json_body); return -1; } if (lws_finalize_write_http_header(wsi, start, &p, end) < 0) { printf("写入HTTP头失败\n"); - free(text1); - free(send); - cJSON_Delete(root); if (json_body) free(json_body); return -1; } @@ -584,9 +564,6 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void /* 写入 JSON 响应体 */ if (lws_write(wsi, (unsigned char *)json_body, json_len, LWS_WRITE_HTTP_FINAL) < 0) { printf("写入响应体失败\n"); - free(text1); - free(send); - cJSON_Delete(root); if (json_body) free(json_body); return -1; } @@ -594,12 +571,7 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void // 释放资源 free(json_body); - cJSON_Delete(root); - } - // 释放解析出的参数 - free(text1); - free(send); } else { /* JSON解析失败 */ printf("JSON解析失败\n"); diff --git a/test.html b/test.html index b696ef1..6959cc2 100644 --- a/test.html +++ b/test.html @@ -124,6 +124,29 @@ font-size: 11px; color: #666; } + .info-tabs { + display: flex; + margin-bottom: 10px; + } + .info-tab { + padding: 8px 15px; + cursor: pointer; + background-color: #e0e0e0; + border: 1px solid #ddd; + border-bottom: none; + border-radius: 4px 4px 0 0; + margin-right: 5px; + } + .info-tab.active { + background-color: #f5f5f5; + font-weight: bold; + } + .info-content { + display: none; + } + .info-content.active { + display: block; + } @@ -141,22 +164,12 @@ - +
-

POST 请求测试

-

填写表单并提交,服务器将返回包含表单数据的 JSON 响应。

- -
- - -
+

获取PCS信息测试

+

点击按钮发送 POST 请求获取PCS信息,服务器将返回包含PCS详细数据的JSON响应。

-
- - -
- - +
@@ -246,29 +259,25 @@ xhr.send(); } - // 只需修改testPost函数,确保它发送有效的JSON格式数据 - function testPost() { + // 获取PCS信息 - 按照pcs.md中的协议规范 + function getPcsInfo() { const postButton = document.getElementById("post-button"); const postLoading = document.getElementById("post-loading"); - const text1 = document.getElementById("post-text1").value; - const send = document.getElementById("post-send").value; - - console.log("准备发送POST请求,数据:", { text1, send }); // 显示加载状态 postButton.disabled = true; postLoading.style.display = "inline"; - showResponse("post-response", "发送 POST 请求中..."); + showResponse("post-response", "发送获取PCS信息请求中..."); // 创建 XMLHttpRequest 对象 const xhr = new XMLHttpRequest(); - xhr.open("POST", "/api/post", true); + // 修改POST请求URL为pcs.md中定义的路径 + xhr.open("POST", "/api/pcs_info", true); // 设置内容类型为 JSON xhr.setRequestHeader("Content-Type", "application/json"); // 设置请求完成回调 xhr.onreadystatechange = function() { - console.log("POST请求状态改变: readyState=", xhr.readyState, ", status=", xhr.status); if (xhr.readyState === 4) { // 隐藏加载状态 postButton.disabled = false; @@ -293,7 +302,7 @@ // 添加错误处理 xhr.onerror = function(error) { - console.error("POST请求错误:", error); + console.error("请求错误:", error); postButton.disabled = false; postLoading.style.display = "none"; showResponse("post-response", "请求发生错误\n" + (error.message || "未知错误")); @@ -302,16 +311,16 @@ // 添加超时处理 xhr.timeout = 10000; // 10秒超时 xhr.ontimeout = function() { - console.error("POST请求超时"); + console.error("请求超时"); postButton.disabled = false; postLoading.style.display = "none"; showResponse("post-response", "请求超时 (超过10秒)"); }; - // 构建JSON数据 + // 构建符合pcs.md协议规范的JSON数据 const jsonData = JSON.stringify({ - text1: text1, - send: send + "code": 0, + "msg": "get_pcs_info" }); console.log("发送的JSON数据:", jsonData); @@ -343,7 +352,7 @@ addMessage("server", "WebSocket 连接已建立"); }; - // 接收消息事件 - 修复:使用专门的处理函数 + // 接收消息事件 webSocket.onmessage = function(event) { console.log("收到 WebSocket 消息: " + event.data); processReceivedMessage(event.data); @@ -385,7 +394,7 @@ } } - // 发送 WebSocket 消息 - 修复:统一使用webSocket变量 + // 发送 WebSocket 消息 function sendWebSocketMessage() { const messageInput = document.getElementById("ws-message"); const message = messageInput.value; -- 2.34.1