C# ajax401错误
C# ajax401错误 解决
C#中ajax代码运行后,报401错误。
代码
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
var txtparam1 = $("#txtParam1").val();
var txtparam2 = $("#txtParam2").val();
$.ajax({
//url: "Ajax.aspx/AjaxMethod",//发送到本页面后台AjaxMethod方法
url: '<%= ResolveUrl("Ajax.aspx/AjaxMethod") %>',
type: "POST",
dataType: "json",
async: true,//async翻译为异步的,false表示同步,会等待执行完成,true为异步
contentType: "application/json; charset=utf-8",//不可少
data: "{param1:'" + txtparam1 + "',param2:'" + txtparam2 + "'}",
success: function (data) {
$("#result").html(data.d);
},
error: function () {
alert("请求出错处理");
}
});
});
});
</script>
<input id="txtParam1" type="text" />
<input id="txtParam2" type="text" />
<input id="btn1" type="button" value="button" />
<div id="result"></div>
后台CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace datashow
{
public partial class Ajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod()]
public static string AjaxMethod(string param1, string param2)
{
return "参数1为:" + param1 + ",参数2为:" + param2;
}
}
}
解决:
找到下面文件打开
~/App_Start/RouteConfig.cs
将下面
settings.AutoRedirectMode = RedirectMode.Permanent;
改为:
settings.AutoRedirectMode = RedirectMode.off;