Ajax调用WebSphere Portal 6.1中的GenericPortlet.serveResource()

BAR*_*BAR 5 jquery portlet websphere-portal jsr286

我正在尝试使用jQuery/ajax调用portlet的serveResource()方法.我设法得到一个简单的JSR-286 portlet,它在Pluto 2.0中工作,能够从请求体读取JSON字符串,从JSON创建一个Java对象,并将该对象的toString()返回给我的调用JavaScript.但是,当我将相同的portlet部署到WebSphere Portal 6.1时,请求体在到达serveResource()时是空的.

我假设我遗漏了一些基本/基本的东西,所以任何建议都会受到赞赏.我认为如果我将JSON字符串推送到URL参数上,但我现在可以避免使用这种方法,我可以让我的示例工作,除非我给出了我当前方法"糟糕"的原因.

编辑:*更具体地说,我将相同的portlet部署到运行WSRP Producer的WAS7并通过WebSphere Portal 6.1使用portlet.

Javascript代码段:

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 />");
}
Run Code Online (Sandbox Code Playgroud)

Portlet代码段:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)