jquery ajax 中 datatype=json 的问题

Hyman行之 2010-03-10 05:21:58
环境: jquery1.4 + struts2.0.14

页面:

<script src="jquery/jquery.js" type="text/javascript"></script>
<script src="jquery/jquery.json-2.2.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function doAjax() {

$.ajax({
type: "post",
url: "ajaxAction.action",
cache:false,
dataType:'json',
success: function(data) { alert($.evalJSON(data).info);},
error:function(){alert('error');}
});
}

</script>



后台:

public class AjaxAction extends ActionSupport {

@Override
public String execute() throws Exception {

HttpServletResponse resp = ServletActionContext.getResponse();
//resp.setContentType("application/json;charset=utf-8");
resp.getWriter().write("{total:100,info:\"ttttttttt\"}");
return NONE;
}

}


问题: 如果在ajax配置项中去掉dataType属性就可以请求成功, 如果设置了dataType=json的情况下就始终执行
error 对应的函数;

我在网上搜索了两个小时,共有如下两种解决方案:
1. 集成struts2的 json-plugin 插件 (google出品), 经过测试:【成功】
2. 改变ajax配置项的contentType为 "application/json" , 后台的response也设置 contentType = "application/json",
经过测试:【失败】

我现在想在不使用json-plugin的情况下,让上面代码中的配置项可以成功跳转到 success 函数。 如果可以顺便讲解一下jquery中ajax的这个dataType的意义,就更好了。。我看jquery源码中的 ajax 函数,没怎么看
明白。。

请各位大虾支点招。谢谢。
...全文
31697 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
tengdengming 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 blackem 的回复:]
你的JSON格式不正确,jquery 1.4以后对json格式变严格了,少了双引号
resp.getWriter().write("{total:100,info:\"ttttttttt\"}");

resp.getWriter().write("{\"total\":\"100\",\"info\":\"ttttttttt\"}");

也就是说必须要这种格式的
{"键":"值",……
[/Quote]
正解
vivi沧海月明 2011-06-02
  • 打赏
  • 举报
回复
17楼正解,1.4版 对json格式要求更加严格了!
另外在服务器端手动拼凑 { } 这种json格式是不推荐的,最后采用自动序列化的方式!
popfancy 2011-04-28
  • 打赏
  • 举报
回复
把 type设置为"get"就行了
fxhchl 2011-04-19
  • 打赏
  • 举报
回复
哈哈,谢了!我也是试了好多次都没解决
fjun123 2011-03-28
  • 打赏
  • 举报
回复

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-javascript;charset=UTF-8");
response.getWriter().write("{\"result\":\"OK!\"}");

<action name="delLogins" class="LoginManager" method="delLogins"></action>


$.ajax({
type : "POST",
url : url,
data : {"ids": ids},
dataType : "json",
success : function(data){
alert(data.result);
}
});

经测试可行!
soular 2011-01-15
  • 打赏
  • 举报
回复
是什么源因
qknyjcel 2010-08-30
  • 打赏
  • 举报
回复
解决方案有没有?这个问题好烦
wbkt6t 2010-07-30
  • 打赏
  • 举报
回复
你的JSON格式不正确,jquery 1.4以后对json格式变严格了,少了双引号
resp.getWriter().write("{total:100,info:\"ttttttttt\"}");

resp.getWriter().write("{\"total\":\"100\",\"info\":\"ttttttttt\"}");

也就是说必须要这种格式的
{"键":"值","键":"值"};

像原来的
{键:值,键:值}
{'键':'值','键':'值'}

这种都是错误的,不合标准,所以jquery返回error


=======================================
正解,谢谢了~~
blackem 2010-07-30
  • 打赏
  • 举报
回复
你的JSON格式不正确,jquery 1.4以后对json格式变严格了,少了双引号
resp.getWriter().write("{total:100,info:\"ttttttttt\"}");

resp.getWriter().write("{\"total\":\"100\",\"info\":\"ttttttttt\"}");

也就是说必须要这种格式的
{"键":"值","键":"值"};

像原来的
{键:值,键:值}
{'键':'值','键':'值'}

这种都是错误的,不合标准,所以jquery返回error
lcgod 2010-07-19
  • 打赏
  • 举报
回复
确实如楼主所言,无论是后台手写json, 还是用类库生成json,只要是设置了dataType:'json',程序就毫无反应,也不报错,个人怀疑jquery的json格式与通用json不符.还望高手解答之...
飞飞0001 2010-06-11
  • 打赏
  • 举报
回复
我也遇到了
煊烨 2010-03-12
  • 打赏
  • 举报
回复
你可以设置成text ,然后自行处理text to json的过程
煊烨 2010-03-12
  • 打赏
  • 举报
回复
jquery 1.4?
在1.4里面
严格JSON模式,默认调用本地的JSON.parse方法 (jQuery.ajax() 文档, Commit 1, Commit 2, Commit 3)
jQuery 1.3和以前的版本曾使用Javascript的eval对引入的JSON解析。1.4版则会使用本地的JSON解析器,前提是如果有本地的解析器可用。它也会对引入的JSON进行校验。所以在jQuery.getJSON方法里,或当一个Ajax请求的dataType是”json”的时候,jQuery会拒绝不合标准的JSON(例如{foo: "bar"})。

Hyman行之 2010-03-12
  • 打赏
  • 举报
回复
谢谢各位的热心解答,好像除了使用了struts-plugin的插件没别的方法解决了。结贴了。
阿非 2010-03-11
  • 打赏
  • 举报
回复
dataType:'json', 这个不用变

加个 data: {},

然后分别设置 resp.setContentType

text/plain

application/json

试一下
unicode 2010-03-11
  • 打赏
  • 举报
回复
要不试试这个函数吧 $.getJSON
Hyman行之 2010-03-11
  • 打赏
  • 举报
回复
引用 4 楼 sandy945 的回复:
resp.setContentType("text/plain");


这样设置也不行,我试了N多方法,始终执行error , 有点无语了。。。
阿非 2010-03-11
  • 打赏
  • 举报
回复
resp.setContentType("text/plain");

Hyman行之 2010-03-11
  • 打赏
  • 举报
回复
不使用 dataType 这种方法,我在发帖之前就已经测试成功了。!
Hyman行之 2010-03-11
  • 打赏
  • 举报
回复
不设置dataType 是成功返回值的。 但是我现在就是想不明白,为什么设置了dataType='json'以后就始终不成功呢。。我在httpWatch里调试都是正常的。 甚至在error函数的第一个参数 resp 里拿到的 resp.responseText 也是正确的。。。

如何才能让服务器返回让dataType可识别的json格式以及 http MIME 呢。。。?
加载更多回复(5)

52,797

社区成员

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

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