struts2+jquery+ajax+json为什么$.post回调函数无法执行

sinat_26662021 2015-09-24 05:26:59
我的代码:
js:
<script type="text/javascript">
function gotClick(){
$.post("../ajax/test.htm",{ajaxField:12},
function(data,statusText){
alert(1);

},
"json");
}
</script>

html:
<body>


<input type="button" value="提交" onclick="gotClick();">

</body>


action:
public String test() {

setReturnString("success");
return returnString;
}

struts.xml:
<package name="ajax" extends="json-default" namespace="/ajax">
<action name="test" class="com.qtj.action.MobileAction" method="test">
<result name="success" type="json"></result>
</action>
</package>

struts2-json-plugin-2.3.8 已经导入。
在后台设断点,显示进入了java,并且ajaxField=12也传入了。
但是function(data,statusText){
alert(1);
},不执行。

求指导,第一次用ajax,不知道问题出在哪里。
...全文
173 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_26662021 2015-09-27
  • 打赏
  • 举报
回复
引用 9 楼 xiaofanku 的回复:
如果是restful api可以不用管.jax-rs 提供不同的方法或用同一个方法(xml/json,plain可能不行)也可
已经解决了!非常感谢! 中秋快乐!
街头小贩 2015-09-26
  • 打赏
  • 举报
回复
如果是restful api可以不用管.jax-rs 提供不同的方法或用同一个方法(xml/json,plain可能不行)也可
街头小贩 2015-09-26
  • 打赏
  • 举报
回复
你在作restful 服务api吗?如果不是话还是建议分不同的包.这样便以管理和维护. 例如 一个同步的包:package com.apobates.servlet 一个异步的包:package com.apobates.servlet.asynchr; 同步的地址: @WebServlet("/channel/build") 异步的地址: @WebServlet("/channel/build/ajax") 如果请求参数有

		resp.setContentType("text/html");// text/javascript||application/json
		resp.setCharacterEncoding("utf-8");
		if(req.getParameter("asyn")!=null){
			System.out.println("[MCM]build channel jump asynchor servlet");
			req.getRequestDispatcher("/channel/build/ajax").forward(req, resp);
		}
sinat_26662021 2015-09-26
  • 打赏
  • 举报
回复
引用 5 楼 xiaofanku 的回复:
贴 一下你的xhr响应瞧瞧
你好!后来我把action用到的方法单独放到一个java里面就好了,貌似是原来的java里面有些不用的参数有影响,百度了下,有人说解决办法是: (在不需要json的字段的get方法上加注解,(推荐使用) 如果在前台页面不需要使用该属性的话,就不要把他传到前台去,设置方法是在其getter方法上加一注解: @JSON(serialize=false) @JSON(serialize=false) public Set getUsers() { return users; } 这样json插件在转换数据时就会忽略该属性。) 因为我原来那个java里面有很多的属性,所以就没试,之前习惯一个java里面写很多的方法,所以属性特别多,所以我想问你一下怎么样的属性是不需要json的属性? 或许一个java里面有些方法需要json,而有些不需要,那么是不是必须把它们分到两个java里面去? 新手不大懂,望指点一下,非常感谢!
sinat_26662021 2015-09-26
  • 打赏
  • 举报
回复
引用 5 楼 xiaofanku 的回复:
贴 一下你的xhr响应瞧瞧
body> <h2>Struts Problem Report</h2> <p> Struts has detected an unhandled exception: </p> <div id="exception-info"> <table> <tr> <td><strong>Messages</strong>:</td> <td> <ol> <li>Positioned Update not supported.</li> <li>java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> <li>org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException</li> </ol> </td> </tr> <tr> <td><strong>File</strong>:</td> <td>com/mysql/jdbc/SQLError.java</td> </tr> <tr> <td><strong>Line number</strong>:</td> <td>1,094</td> </tr> </table> </div>
sinat_26662021 2015-09-25
  • 打赏
  • 举报
回复
引用 2 楼 xiaofanku 的回复:
你好!现在在XHR里面有这样的响应。百度下说是json序列化造成的,需要排除action中的不要被JSON序列化的属性的get()方法,我想问一下怎么样的属性是不要被JSON序列化的属性?就是在调用的方法中没有用到的属性吗? Struts Problem Report Struts has detected an unhandled exception: Messages: Positioned Update not supported. java.lang.reflect.InvocationTargetException org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set: 。。。 struts.devMode=false in your WEB-INF/classes/struts.properties file.
sinat_26662021 2015-09-25
  • 打赏
  • 举报
回复
谢谢你!!但是还是有点问题
街头小贩 2015-09-25
  • 打赏
  • 举报
回复
贴 一下你的xhr响应瞧瞧
街头小贩 2015-09-24
  • 打赏
  • 举报
回复
街头小贩 2015-09-24
  • 打赏
  • 举报
回复
1.

<script type="text/javascript">
function gotClick(){
        $.post("../ajax/test.htm",{ajaxField:12},function(data){
                alert(1);

        },"json");
}
</script>
2.调试ajax,最好用chome或firefox打开开发者控制台中的xhr。

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧