Skip to content

Commit

Permalink
输出Jsonp 实现跨域Ajax调用.
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Feb 23, 2012
1 parent 585f8aa commit 1cd6efc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 1cd6efc

Please sign in to comment.