C# 当方法具有参数时,Ajax回调失败
我有以下javascript:C# 当方法具有参数时,Ajax回调失败,c#,javascript,asp.net,ajax,C#,Javascript,Asp.net,Ajax,我有以下javascript: <script type="text/javascript"> $(document).ready(function() { $.ajax({ type: "POST", url: "WebForm3.aspx/sayHello", contentType: "application/json; charset=utf-8", dat
<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>
这可以工作并显示hello
现在,如果我这样做:
[WebMethod]
public static string sayHello(string args)
{
return "hello";
}
然后我得到内部服务器错误500作为答复
如何更改此设置以允许我将实际数据发送到服务器端
data: {
args: "hello world"
},
另外,还要删除contentType
和dataType
。另外,请添加
传统:true
。您的最终请求将是:
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
traditional: true,
data: {
args: "hello world"
},
success: AjaxSucceeded,
error: AjaxFailed
});
另外,还要删除contentType
和dataType
。另外,请添加
传统:true
。您的最终请求将是:
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
traditional: true,
data: {
args: "hello world"
},
success: AjaxSucceeded,
error: AjaxFailed
});
那么
这项工作:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm3.aspx/Test",
data: '{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
}
});
});
那么
这项工作:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm3.aspx/Test",
data: '{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
}
});
});
您最好启用调试模式。错误消息有助于:)并且您最好启用调试模式。错误消息帮助:)@Milo不指定
contentType
选项too@Milo也不要指定数据类型
选项。这意味着“我希望响应的格式是JSON”。在您的情况下,它不是…它是“text/html”(通常是web服务器使用的默认值),值是“hello world”。现在删除该消息是“undefined”,并将text/html放在其中,会得到200OK@Milo不要指定contentType
选项too@Milo也不要指定数据类型
选项。这意味着“我希望响应的格式是JSON”。在您的情况下,它不是…它是“text/html”(通常是web服务器使用的默认值),其值是“hello world”。现在删除该消息是“undefined”,并放置text/html,我就可以确定了