关于JQuery 和 WS ScriptMethod(UseHttpGet = true)?

sskset 2011-06-22 12:48:21
请问下,我在WebService方法里定义了[ScriptMethod(UseHttpGet = true)],
然后在JQuery里调用的时候为什么还是要POST才能够取到值?

MSDN 也说是要用GET取值
http://msdn.microsoft.com/zh-cn/library/system.web.script.services.scriptmethodattribute.usehttpget.aspx

在线等到答案就结贴...谢谢啊



Web Service 代码


[ScriptService]
[WebService(Namespace = "http://www.midaske.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SyncService : System.Web.Services.WebService
{
public SyncService()
{

}

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Hello()
{
return "Hello Test";
}
}



JS代码


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript" language="javascript">
$.ajax({
url: "Services/SyncService.asmx/Hello",
type: "post",
dataType: ($.browser.msie) ? "text" : "xml",
success: function (data) {
alert("success");
var xml;
if (typeof data == "string") {
alert("1");
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
alert("2");
xml = data;
}

var ser = new XMLSerializer();
alert(ser.serializeToString(xml));
},
error: function (e) {
alert("error");
alert(e.toString());
}
});

</script>
</body>
</html>




...全文
235 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-06-22
  • 打赏
  • 举报
回复
参考

<script type="text/javascript">

$(function () {

/*
1、WebService请求类型都为Post,WebService的Url为“[WebServiceUrl]/[WebMethod]”
2、contentType声明为Json
3、data要用Json的字符串格式传入
4、设置了dataType为json后,result就直接为返回的Json对象。

*/

//调用无参数方法
$("#btnHelloWorld").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/HelloWorld",
data: "{}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
});

//传入1个参数
$("#btnHello").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/Hello",
data: "{name:'KiMoGiGi'}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
});

//返回泛型列表
$("#btnArray").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/CreateArray",
data: "{i:10}",
dataType: 'json',
success: function (result) {
alert(result.d.join(" | "));
}
});
});

//返回复杂类型
$("#btnPerson").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetPerson",
data: "{name:'KiMoGiGi',age:26}",
dataType: 'json',
success: function (result) {
var person = result.d;
var showText = [];
for (var p in person) {
showText.push(p + ":" + person[p]);
}
alert(showText.join("\r\n"));
}
});
});
});

</script>
HDNGO 2011-06-22
  • 打赏
  • 举报
回复
data:"",
这个加上试试滴~~
HDNGO 2011-06-22
  • 打赏
  • 举报
回复
无参数?GET?
不用那么复杂吧。。。

$.ajax({
url: "WebService1.asmx/WS3",
dataType: "json",
data: "",
beforeSend: function(x) {
x.setRequestHeader("Content-Type", "application/json; charset=utf-8");
},
success: function(json) {
alert(json.d);
},
error: function(x, e) {
alert(x.responseText);
},
complete: function(x) {
alert(x.responseText);
}
});
Thomas_Chen 2011-06-22
  • 打赏
  • 举报
回复
接分 下载要分却没分真可悲
q107770540 2011-06-22
  • 打赏
  • 举报
回复
设置 ScriptMethod(UseHttpGet = true) 为GET方法请求的方法吧。。

type: "GET",

参考一下这个:
http://www.cnblogs.com/sk-net/archive/2011/06/17/2083885.html

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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