在 WebSphere Portal 6.1 中对 GenericPortlet.serveResource() 的 Ajax 调用
我正在尝试使用 jQuery/ajax 调用 portlet 的serveResource() 方法。我设法获得了一个在 Pluto 2.0 中工作的简单 JSR-286 portlet,它能够从请求正文中读取 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 />");
}
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);
}
}
I'm trying to call a portlet's serveResource() method using jQuery/ajax. I managed to get a simple JSR-286 portlet working in Pluto 2.0 that is able to read a JSON string from the request body, create a Java object from the JSON, and return that object's toString() to my calling JavaScript. However when I deploy* the same portlet to WebSphere Portal 6.1 the request body is empty by the time it reaches serveResource().
I'm assuming I'm missing something basic/fundamental so any advice would be appreciated. I think I could get my sample working if I pushed the JSON string onto the URL parameters but would prefer to avoid that approach for now, unless I'm given a reason why my current approach is "bad".
Edit: *To be more specific, I deployed the same portlet to WAS7 running a WSRP Producer and consuming the portlet via 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 Snippet:
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于让它工作了......有点。
通过将我的 ajax 调用中的 contentType 参数更改为“application/x-www-form-urlencoded”(并使用不同的方法来表示我的数据),我现在可以在我的 WebSphere 环境中的帖子正文中使用我的数据,尽管是在URL 参数形式而不是 JSON。
不幸的是,执行此更改会导致 Pluto 中的功能被破坏。该环境中的请求正文现在为空。
现在,要么更改代码以从 request.getParameter() 检索数据(我相信在我的更改下,这在两种环境中都有效,但需要进一步测试),要么查找将在两种环境中生成填充请求正文的 contentType。
Finally got it working...sort of.
By changing the contentType parameter in my ajax call to 'application/x-www-form-urlencoded' (and playing with different methods of representing my data) I now have my data available in the post body in my WebSphere environment, albeit in a URL parameter form as opposed to JSON.
Unfortunately, performing this change has resulted in breaking the functionality in Pluto. The request body in that environment is now empty.
Now to either change the code to retrieve the data from request.getParameter() (which I believe works in both environments with my change but requires further testing) or finding a contentType that will result in a populated request body in both environments.
当您发布 JSON 请求时,数据类型必须是 JSON。
数据类型:'JSON',
As you are posting a JSON request dataType must be JSON.
dataType: 'JSON',