使用Ajax访问后台方法,返回的值为什么是undefined
【求助】使用Ajax访问后台方法,返回的值为什么是undefined?
前台代码
后台代码
Javascript代码
------解决思路----------------------
$.ajax({
type: "post",
url: "../JQueryTCSharp/JQueryTCsharp.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function (err) {
alert(err);
}
});
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JQueryTCsharp.aspx.cs" Inherits="JQueryTCSharp_JQueryTCsharp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/ASP.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="JS" value="调用" />
</div>
</form>
</body>
</html>
后台代码
using System.Web.Services;
using System.Web.Script.Services;
public partial class JQueryTCSharp_JQueryTCsharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string SayHello()
{
return "Xyb Good!!";
}
}
Javascript代码
$(function () {
$("#JS").click(function () {
$.ajax({
type: "post",
url: "../JQueryTCSharp/JQueryTCsharp.aspx/SayHello",
contenType: "application/json; charset=utf-8",
dataaType: "text",
success: function (data) {
alert(data.d);
},
error: function (err) {
alert(err);
}
});
return false;
});
});
------解决思路----------------------
$.ajax({
type: "post",
url: "../JQueryTCSharp/JQueryTCsharp.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function (err) {
alert(err);
}
});