I'm trying to make simple Ajax request with following code in main.js
file:
$.ajax({
type: "POST",
url: "SaveStyles.aspx/GetDate",
data: { someParameter: "some value" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
console.log(msg);
}
});
In C# in SaveStyles.aspx.cs
I have method:
[WebMethod]
public static string GetDate(string someParameter)
{
return "Sup!";
}
But in JS console I see error saying that authentication is failed with System.InvalidOperationException
. I have no idea why it's happening. Mabye I shoud add something like anti-csrf
token?
Resolved by setting AutoDirectMode to Off in App_Start/RouteConfig.cs
settings.AutoRedirectMode = RedirectMode.Off;
and adding a ScriptManager to the aspx page that has an EnablePageMethods set to 'true':
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.