1

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
Varun Rajput
  • 235
  • 1
  • 7
  • I'm surprised you end up with `body=+`. I would have expected something like `body=[object HTMLFormElement]` since you're converting an object representing an HTML element into a string. – Ouroborus May 28 '22 at 03:13

1 Answers1

1

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.

Chris Schankula
  • 303
  • 1
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '22 at 17:40