输出Jsonp 实现跨域Ajax调用.

This commit is contained in:
Calvin
2012-02-23 23:45:38 +08:00
parent 585f8aa3e5
commit 1cd6efc5cb
3 changed files with 13 additions and 7 deletions
@@ -3,25 +3,31 @@ package org.springside.examples.showcase.web;
import java.util.Collections;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.mapper.JsonMapper;
/**
* JSONP Mashup 服务端.
*
* @author calvin
*/
@RequestMapping(value = "/mashup")
@Controller
public class MashupServerController {
public String execute() {
//获取JQuery客户端动态生成的callback函数名.
//String callbackName = Struts2Utils.getParameter("callback");
private JsonMapper mapper = JsonMapper.buildNormalMapper();
@RequestMapping("/web/mashup")
@ResponseBody
public String execute(@RequestParam("callback") String callbackName) {
//设置需要被格式化为JSON字符串的内容.
Map<String, String> map = Collections.singletonMap("html", "<p>Hello World!</p>");
//渲染返回结果.
// Struts2Utils.renderJsonp(callbackName, map);
return null;
return mapper.toJsonP(callbackName, map);
}
}
@@ -22,7 +22,7 @@
3. CacheControlHeaderFilter为静态内容添加缓存控制 Header<br/>
4. YUI Compressor 压缩js/css<br/>
</li>
<li><a href="${ctx}/web/mashup-client.action">跨域名Mashup演示</a> 演示基于JSONP Mashup 跨域名网站的内容.</li>
<li><a href="${ctx}/story/web/mashup-client">跨域名Mashup演示</a> 演示基于JSONP Mashup 跨域名网站的内容.</li>
</ul>
<h3>用户故事:</h3>
@@ -10,7 +10,7 @@
//使用JQuery.getJSON(url?callback=?)方式跨域名访问内容
function fetchMashupContent() {
$.getJSON(remoteUrl + "/web/mashup/mashup-server.action?callback=?", function(data) {
$.getJSON(remoteUrl + "/web/mashup?callback=?", function(data) {
$('#mashupContent').html(data.html);
$('#mashupContent').show();
});