IHP-无法从请求正文中检索特殊字符

发布于 2025-02-01 08:54:12 字数 815 浏览 3 评论 0原文

我正在尝试使用AJAX发送请求:

    const formBody = document.getElementById('body'); // my form data
    const XHR = new XMLHttpRequest();
    const params = "body=" + formBody;
    
    XHR.open("POST", window.origin + '/CreateFormAction');
    XHR.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    XHR.send(params);

相应的IHP操作:

action CreatePostAction = do
        rBody <- getRequestBody
        putStrLn $ tshow rBody -- this returns: ""
        renderPlain "Request Received"

当我尝试发送诸如'$','+'等的特殊字符时,这是我在服务器上收到的请求:

POST /CreatePostMessage
  Params: [("body"," ")]
  Request Body: body=+
  Accept: */*
  Status: 200 OK 0.025023s

I'm trying to send the request using ajax:

    const formBody = document.getElementById('body'); // my form data
    const XHR = new XMLHttpRequest();
    const params = "body=" + formBody;
    
    XHR.open("POST", window.origin + '/CreateFormAction');
    XHR.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    XHR.send(params);

The corresponding IHP action:

action CreatePostAction = do
        rBody <- getRequestBody
        putStrLn $ tshow rBody -- this returns: ""
        renderPlain "Request Received"

When I try sending special characters like '$', '+', etc., this is the request I get on server:

POST /CreatePostMessage
  Params: [("body"," ")]
  Request Body: body=+
  Accept: */*
  Status: 200 OK 0.025023s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2025-02-08 08:54:12

您必须使用encodeuricomponent()对FormBody进行编码,以编码特殊字符,如下所示:

const params = "body=" + encodeURIComponent(formBody);

您的IHP操作应该能够处理特殊字符。

You have to encode the formBody using encodeURIComponent() to encode special characters as follows:

const params = "body=" + encodeURIComponent(formBody);

Your IHP action should be able to handle special characters then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文