【发布时间】:2012-04-23 00:32:10
【问题描述】:
代码 1
$.ajax({url:"1.aspx/HelloWorld",type:"GET",dataType:"json",contentType:"application/json"});
代码 2
$.ajaxSetup({
contentType: "application/json",
dataType: "json"
});
$.get("1.aspx/HelloWorld","",$.noop,"json");
Code1 有效地设置了content-type 和datatype
Code2 没有设置内容类型这是有意的还是我必须做 Voodoo 的东西才能使它工作?
【问题讨论】:
-
代码 2 的
$.get可以重写为$.get(url),因为您没有传递数据或回调函数,并且由于 ajaxSetup,json 是默认的 dataType。 -
@Starx 大声笑这是
Get请求,而不是帖子。帖子确实很好用 -
@KevinB Content-Type 和 dataType 在这里意思一样吗?如果是,那么你说的是正确的。但我认为 content-Type 指定了请求中预期的数据的 mime 类型
-
@Deeptechtons 正确,但是如果您已经在 ajaxSetup 中将其定义为默认值,则在 $.get 中指定“json”是没有意义的。保持 ajaxSetup 不变,使用缩短的
$.get(),结果应该是一样的。你用的是什么版本的jQuery?
标签: jquery asp.net ajax content-type pagemethods