方法调用不好

时间:2011-12-23 14:17:46

标签: c# ajax visual-studio-2010

以下是我的html文件的一部分:

$('#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);

1 个答案:

答案 0 :(得分:0)

如果您尝试使用支持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方法本身内置于现代浏览器中,但如果您需要支持旧版浏览器,则可能需要包含json2.js脚本。

相关问题