我想通过ajax调用将数据发布到服务器,但出现错误。
var userdata = {}; userdata["Name"] = "Saran"; var DTO = { 'userdata': userdata }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/update", data: JSON.stringify(DTO), datatype: "json", success: function (result) { //do something alert("SUCCESS = " + result); console.log(result); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(" conection to the server failed "); console.log("error: " + errorthrown); } });//end of $.ajax()我在Default.aspx.cs中创建了一个函数,并试图通过上面的调用访问该函数。
[WebMethod] public static string update(string userdata) { return "Posted"; }错误:
POST http://localhost:33762/Default.aspx/update 401 Unauthorized 52ms消息“身份验证失败。” StackTrace null ExceptionType “System.InvalidOperationException”
I want to post data to server with ajax call but i am getting an error.
var userdata = {}; userdata["Name"] = "Saran"; var DTO = { 'userdata': userdata }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/update", data: JSON.stringify(DTO), datatype: "json", success: function (result) { //do something alert("SUCCESS = " + result); console.log(result); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(" conection to the server failed "); console.log("error: " + errorthrown); } });//end of $.ajax()I have created a function in Default.aspx.cs and tried to access that function with the above call.
[WebMethod] public static string update(string userdata) { return "Posted"; }Error :
POST http://localhost:33762/Default.aspx/update 401 Unauthorized 52msMessage "Authentication failed." StackTrace null ExceptionType "System.InvalidOperationException"
最满意答案
首先,你必须设置/更新settings.AutoRedirectMode = RedirectMode.Off; 在App_Start / RouteConfig.cs中 。
其次,您的ajax有效负载的结构不正确,无法正确调用update方法。 请参阅以下更新:
var DTO = { 'userdata': 'Saran' }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/update", data: JSON.stringify(DTO), datatype: "json", success: function (result) { //do something alert("SUCCESS = " + result.d); console.log(result); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(" conection to the server failed "); console.log("error: " + errorthrown); } });//end of $.ajax()Firstly, you have to set/update to settings.AutoRedirectMode = RedirectMode.Off; in App_Start/RouteConfig.cs.
Secondly, your ajax payload is not structured properly to make the appropriate call to the update method. See updates below:
var DTO = { 'userdata': 'Saran' }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/update", data: JSON.stringify(DTO), datatype: "json", success: function (result) { //do something alert("SUCCESS = " + result.d); console.log(result); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(" conection to the server failed "); console.log("error: " + errorthrown); } });//end of $.ajax()更多推荐
ajax,Default,aspx,function,update,电脑培训,计算机培训,IT培训"/> <meta name=&
发布评论