【问题标题】:Spring dojo request issueSpring dojo 请求问题
【发布时间】:2013-08-22 03:45:09
【问题描述】:

我对 Spring 框架很陌生,但遇到了问题。 我有一个页面 A.jsp,在这个页面中我有一个指向页面 B.jsp 的链接

<c:url value="${pageContext.request.contextPath}" var="contextPath" />
Click <a href="${contextPath}/pageB">here</a>

在控制器中

@RequestMapping("pageB")
public String pageBlink(SitePreference sitePreference, Device device, Model model) {
return "pageB";     
}

现在在 B.jsp 页上,我想调用 Ajax 调用。

I have a link <a href="javascript:myFunction();">Send request</a>

function myFunction(){
dojo.xhrGet({
// The URL of the request
url: "requestPage",
method: "POST",
handleAs: "json",
// The success callback with result from server
load: function(jsonData) {
var content = "";
                                dojo.forEach(jsonData.newsItems,function(locationPoint) {
// Build data from the JSON
content += "<p>" + locationPoint.name + "</p>";
content += "<p>" + locationPoint.latitude + "</p>";
content += "<p>" + locationPoint.longitude + "</p>";
content += "<p>" + locationPoint.number + "</p>";
});
},
// The error handler
error: function() {
// Do nothing -- keep old content there
},
// generate an extra GET variable to prevent browsers from caching
preventCache: true
});
}

并添加到控制器中

@RequestMapping(value="requestPage", method = RequestMethod.GET)
public MyObj returnEVSELocations(){
    logger.log(Level.INFO, "return evse locations --------------");
    MyObj myObj = new MyObj();
    // add some stuff into the obj

    return myObj;
}

但是这个请求是 requestPage.jps ...我只想在我的页面 (B.jsp) 中工作。 任何帮助都非常受欢迎。 谢谢!

【问题讨论】:

  • 我不明白你的最后一句话包含问题以及它应该是什么,所以很难给出答案。你能提供更多信息吗?
  • 我的意思是在控制台中的错误消息说没有找到 requestPage.jsp 页面。我想在同一页面中检索响应。

标签: ajax json spring-mvc dojo


【解决方案1】:

我发现了问题。 实际上有2个问题 1.在Ajax调用中我必须有 dojo.forEach(jsonData, ...) 而不是 dojo.forEach(jsonData.newsItems, ...) 2.在我的方法的控制器中,我必须添加注释

public @ResponseBody MyObj 

我希望这可以帮助某人解决同样的问题。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2021-01-10
  • 2011-09-06
  • 1970-01-01
  • 2020-05-08
  • 2011-02-06
  • 2011-09-02
相关资源
最近更新 更多