简体   繁体   中英

How to retrieve parameters from POST in XPages (java)

I want to use Domino as a backend, and html/jquery as a frontend for my web application. So I have:

$.ajax({
    type: 'POST',
    url: 'mydb.nsf/xpage.xsp',
    contentType: 'application/json; charset=utf-8',
    data: { 
        f1: "hello", 
        f2: "hello again"
    },
    success: function (response) {
        console.log("SUCCESS");
    },
    error: function (error) {
        console.log(error);
    }
});

In Domino:

public static String doGet(HttpServletRequest req, HttpServletResponse res) throws JsonException, IOException, NotesException {
    return doPost(req, res);
}    

public static String doPost(HttpServletRequest req, HttpServletResponse res) throws JsonException, IOException, NotesException {
    System.out.println("1) "+req.getAttribute("f1"));
    System.out.println("2) "+req.getParameter("f1"));
    System.out.println("3) "+req.getContentLength());

    return "AllOK";
}

In firebug and domino log I see that POST goes trough ok, gets the response. But I can't figure out how to get params f1 and f2 in domino. In domino log: 1) is null, 2) is null, 3) is 23.

Idea for later is to POST JSON, but for now it would be great to have this code working.

How to get POST parameters in domino via java?

(I see stackoverflow has a lot of similar questions answered, but couldn't find anything specific to my problem)

Thank you!

Use reg.getReader() or req.getInputStream() to read the body of the request with elements f1 and f2.

Here is an example how you can read the JSON data: https://stackoverflow.com/a/3831791/2065611

req.getParameter() works only if content type is "application/x-www-form-urlencoded", not json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM