C# 方法调用错误
以下是我的html文件的一部分:C# 方法调用错误,c#,ajax,visual-studio-2010,C#,Ajax,Visual Studio 2010,以下是我的html文件的一部分: $('#btnMyButton').click(function () { alert("message"); ajaxPost("Service1.svc/json/GetMethod", { "humanName": "anna" }, anotherMethod); }); 下面是调用的方法: public Human GetCustomer(string humanName) { Huma
$('#btnMyButton').click(function () {
alert("message");
ajaxPost("Service1.svc/json/GetMethod", { "humanName": "anna" }, anotherMethod);
});
下面是调用的方法:
public Human GetCustomer(string humanName)
{
Human x = new Human();
x.name = humanName;
return x;
}
但我得到了错误-400个错误的请求!如何修复它
anotherMethod方法的主体是:
var txt = JSON.stringify(msg);
log("Result = " + txt);
以下是方法的名称:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
Human GetMethod(string humanName);
如果您试图使用支持JSON的WCF web服务,可以尝试以下操作:
$('#btnMyButton').click(function () {
$.ajax({
url: 'Service1.svc/json/GetMethod',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ humanName: 'anna' }),
success: function(result) {
// TODO: do something with the results
}
});
return false;
});
这里显示的
JSON.stringify
方法是内置于现代浏览器中的,但如果您需要支持传统浏览器,则可能需要包含脚本。@Srcee,您必须提供有关服务的更多详细信息。它接受POST方法吗?它接受JSON请求吗?它是如何配置的。。。在你的问题接近可以回答之前,有很多细节是必要的。现在我们只能猜测了。@Srcee,关于您的服务的详细信息。@Srcee,关于您的web.config呢?端点配置为侦听的url是什么?如果方法的参数和返回类型是人工的,则一切正常。如果输入类型是string,那就不好了:(这就是为什么我认为web.config不是问题所在。