我遇到以下问题:每当我点击button
时,alert
都会显示undefined
。
网络方法:
[WebMethod]
public static string getTest()
{
return "testing success";
}
Ajax脚本
<script type="text/javascript">
function getTest() {
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
datatype: "json",
contenttype: "/application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
}
</script>
答案 0 :(得分:1)
datatype
应为dataType
,contenttype
应为contentType
。同时从"/application/json; charset=utf-8"
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
答案 1 :(得分:0)
从你的ajax电话中删除这些
datatype: "json",
contenttype: "/application/json; charset=utf-8",
有关这些的更多信息
Differences between contentType and dataType in jQuery ajax function
中找到更多详情