jsonp跨域处理例子

最近我换工作,由于我面试能力特别差,简历也写不好,原公司我比较尊敬的leader(我经常叫他南哥,很有陈浩南的感觉,让我也觉得我在混江湖很有意思,小时候估计都想做一个厉害的古惑仔)叫我有空写写博客,从此我就将写blog,一切都归咎于我记忆力差,理解能力还可以,理解了就不去记了不知该不该认为是我的缺点,所以原公司的项目面试的时候问了也会经常答不出来,总是被鄙视心情也不太好。嘛!总有一天写代码将成为我的兴趣,不再以写代码为生是我的目标,我喜欢研究代码,但以此作为生存工具,我认为违背了我的初衷,失去一项爱好就相当于看完一部好看的电影,会为何时才能再出现这样的电影而感伤。


回到正题

代码如下:

1.html+jquery+jsonp:

2.struts2中xml文件:

3.action的java代码:

<!DOCTYPE html>
<html>

	<head>

		<meta charset="UTF-8">

		<title></title>
		<script type="text/javascript" src="jquery2.0.3/jquery-2.0.3.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("#test").click(function() {
					$.ajax({
						type:'get',
						url: 'http://127.0.0.1:8080/TestWeb/testjson',
						dataType: 'jsonp',
						jsonp:'callback',
						jsonpCallback:'callbackMethod',
                                                success: function() {
							alert("成功");
						},
						error: function() {
							alert("失败!");
						}
					});
				});
			});
		</script>
	</head>

	<body>
		<button id="test">测试</button>
	</body>

</html>

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
 * $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
	<package name="default" namespace="/" extends="json-default">
		<action name="testjson" class="com.test.action.HelloWorld" method="test2">
			<result type="json">
				<param name="callbackParameter">callback</param>
				<param name="message">this is a json</param>
			</result>
		</action>
	</package>
</struts>

	public String test2() {
		return SUCCESS;
	}


callback是回调函数的参数,这里可能会理解错误,等会贴图,callbackMethod是回调函数名,我们可以看看浏览器url如下:


我们再看下返回值

callbackParameter这个配置是必须的,另外一点值得注意的是,这个是不能用注解的
//	@Action(value = "testjson", results = { @Result(name = "success", type = "json") }, params = { "callbackParameter",
//			"callback", "message", "this is a json"})
	public String test2() {
		return SUCCESS;
	}

这样是没有效果的,不知struts2注解要怎么使用跨域,希望哪位大神知道的可以讲解一下。
今天到此为止,还要去面试,继续被人虐!

最后,引用以及转载请标明出处,谢谢各位!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值