关于Ajax 的, ,报错 啦,
: 找不到类型或命名空间名称“WebMethod”(是否缺少 using 指令或程序集引用?)
前台JS 代码,
<script>
$().ready(
function() {
$("#AjaxDemo").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/ABC",
data: "{'ABC':'test'}",
contentType: "application/json; charset=utf-8",
success: function(msg) {alert(msg); }
})
})
}
)
</script>
后台Default.aspx.cs:
using System.Web.Script.Services;
public partial class JStest_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string ABC(string ABC)
{
return ABC;
}
}
请大神给个完整的AJSX应用例子, 在网上找有人写在一般程序中,有些又直接写在后台。。 为何,,
[解决办法]
命名空间:System.Web.Services
程序集:System.Web.Services(在 system.web.services.dll 中)
[解决办法]
http://www.cnblogs.com/youmeng/archive/2012/12/19/2825622.html
前台
<script type="text/javascript">
$(function () {
$("#btnAjax").click(function () {
$.post("Ajax.ashx?action=Show", {
ID: "2012"
}, function (data) {
var json = eval("(" + data + ")"); //对返回的json数组进行解析
alert(json.ID);
});
});
});
</script>
public class ajax : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string action = context.Request.QueryString["action"];
switch (action)
{
case "Show":
Show(); break;
}
}
protected static void Show()
{
System.Threading.Thread.Sleep(3000);
string ID = HttpContext.Current.Request["ID"];
WriteJson("ID", ID);
}
/// <summary>
/// 输出Json
/// </summary>
/// <param name="key"></param>
/// <param name="val"></param>
private static void WriteJson(string key, string val)
{
HttpContext.Current.Response.Write(GetJSON(key, val));
HttpContext.Current.Response.ContentType = "text/plain"; //设置MIME格式
HttpContext.Current.Response.End();
}
/// <summary>
/// 获取JSON字符串
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
private static string GetJSON(string key, string val)
{
return string.Format("{{"{0}":"{1}"}}", key, val);
}
public bool IsReusable
{
get
{
return false;
}
}