Ajax call GenericPortlet.serveResource () in WebSphere Portal 6.1

I am trying to call the portere serveResource () method using jQuery / ajax. I was able to get a simple JSR-286 portlet running in Pluto 2.0 that can read a JSON string from a request body, create a Java object from JSON and return this toString () object to my JavaScript call. However, when I deploy * the same portlet in WebSphere Portal 6.1, the request body is empty by the time it reaches serveResource ().

I assume that I am missing something basic / fundamental, so any advice would be appreciated. I think I could make my model work if I push the JSON string to the URL parameters, but would prefer to avoid this approach for now, unless I have been given a reason why my current approach is “bad”.

Edit: * To be more specific, I deployed the same portlet to WAS7 where WSRP Producer works, and used the portlet through WebSphere Portal 6.1.

Javascript Snippet:

function ajaxPost() {
    var url = "<%= testServiceURL %>";
    var current = $("input.current").val();
    $.ajax(
        {
            url: url,
            contentType: 'application/json; charset=utf-8',
            dataType: 'html',
            data: "{data: " + current + "}",
            type: 'POST',
            success: testSuccess,
            error: testError
        }
    );
    $("div.trace").append("ajax post fired<br />");
}

function testSuccess(data, textStatus, XMLHttpRequest)
{
    $("div.trace").append("testSuccess(): " + data + "<br />");
}

Portlet Fragment:

public class TestPortlet extends GenericPortlet {
    ...
    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
        String res = "Failed to read body";

        boolean bodyRead = true;
        StringBuffer sb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            line = reader.readLine();
            while (line != null) {
                sb.append(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            bodyRead = false;
        }

        Foo f = null;
        if (bodyRead) {
            try {
                Gson gson = new Gson();
                f = gson.fromJson(sb.toString(), Foo.class);
                res = "Received: " + f.toString();
            } catch (Exception e) {
                res = "Failed to convert body into Foo: '" + sb.toString() + "'";
            }
        }

        response.setContentType("text/html");
        response.getWriter().println(res);
    }
}
+5
source share
2 answers

It finally worked ... sort of.

contentType ajax "application/x-www-form-urlencoded" ( ), WebSphere, URL, JSON.

, Pluto. .

, request.getParameter() (, , , ) contentType, .

+1

JSON dataType JSON.

dataType: 'JSON',

0

All Articles