我使用jquery并希望将json对象发送到包含Web方法的aspx文件,它总是返回http 500。如何重写代码?我想我在cs文件中写了一些错误,但我不知道如何解决。谢谢
var empInfo= [
{ Name1: "A1", Age: 25, Address: "aaaaaaaaaaaaaaaaaa" },
{ Name1: "B1", Age: 35, Address: "bbbbbbbbbbbbbbbbbb" },
{ Name1: "C1", Age: 45, Address: "cccccccccccccccccc" },
{ Name1: "D1", Age: 55, Address: "dddddddddddddddddd" },
{ Name1: "E1", Age: 65, Address: "eeeeeeeeeeeeeeeeee" }
];
$(document).ready(function () {
$.ajax({
method:'POST',
async: true,
contentType:'application/json;charset=utf-8',
crossDomain: true,
dataType: 'json',
data: JSON.stringify(empInfo),
url: 'WebForm2.aspx/Method5',
timeout: 30000,
success: function (data) {
console.log('success');
//console.log(data.d);
},
error: function (status) {
console.log('fail..');
},
statusCode: {
404: function () { alert('404');},
500: function () { alert('500') },
200: function () { alert('200')}
}
});
});
------后端
public class empInfo
{
public string Name1 { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
[WebMethod(EnableSession =true)]
public static void Method5(empInfo[] emp)
{
.......
.......
}
大多数人会遇到以下相对于当前路径或域的 url 问题
在您的情况下,将url: 'WebForm2.aspx/Method5'
更改为 url: '/WebForm2.aspx/Method5',
可能会解决您的问题
请参阅 WebForm2 前面的附加反斜杠"/.aspx