您应该必须使用 'json' dataType 将数据传递给 web 方法。
看看样本:
服务.asmx
[ScriptService]
[WebService(Namespace = "http://localhost/testapp/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService{
[WebMethod]
public int Square(int no){ return no * no;}
}
和 JavaScript 代码来请求这个webmethod
<script type="text/javascript">
$(function () {
$("#button1").click(function () {
$.ajax({
type: "post",
url: "service.asmx/Square",
data: "{no: 10}",
dataType: "json",
contentType: "application/json",
success: function (data) {
alert("Result :" + data.d);
},
error: function (src,type,msg) {
alert(msg); //open JavaScript console for detailed exception cause
}
});
});
});
</script>
<body>
<input type="button" id="button1" value="Square" />
</body>