jma*_*erx 0 javascript c# asp.net ajax
我有以下javascript:
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的代码背后有以下内容.
[WebMethod]
public static string sayHello()
{
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
这工作并显示你好.
现在,如果我这样做:
[WebMethod]
public static string sayHello(string args)
{
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
然后我得到内部服务器错误500作为回复.
如何更改此设置以允许我将实际数据发送到服务器端?
data: {
args: "hello world"
},
Run Code Online (Sandbox Code Playgroud)
此外,同时删除contentType并dataType为好.
另外,请添加traditional: true.您的结果请求将是:
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
traditional: true,
data: {
args: "hello world"
},
success: AjaxSucceeded,
error: AjaxFailed
});
Run Code Online (Sandbox Code Playgroud)