Ajax wcf服务json 400错误请求

Ajax wcf服务json 400错误请求,ajax,json,wcf,Ajax,Json,Wcf,当我尝试使用ajax从客户端调用WCF服务时,我收到了一个400错误的请求错误。下面是我的代码 [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] string[] GetUser(string Id); $.ajax({ type: "POST", //G

当我尝试使用ajax从客户端调用WCF服务时,我收到了一个400错误的请求错误。下面是我的代码

[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);

$.ajax({
                type: "POST", //GET or POST or PUT or DELETE verb
                url: "http://localhost:58055/Service1.svc/GetUser", 
                crossDomain: true,
                data: '{"Id": "3"}',
                contentType: "application/json; charset=utf-8", 
                dataType: "json", //Expected data format from server
                processdata: true, //True or False
                success: function (msg) {//On Successfull service call
                    alert(msg.GetUserResult[0]);
                    console.log("success " + msg);
                },
                error: function (msg) {//On Successfull service call
                    console.log(msg);
                }
            });

任何洞察都会非常有用…

您应该尝试的第一件事是使用Fiddlers点击URL,这样您也可以发布数据,并查看是否出现相同的错误

您正在进行跨域请求。从这个例子看来,你不是。你能搬走吗

crossDomain: true,
行,然后重试jquery

还有其他不必要的选项,如processdata。建议您使用以下代码,看看它是否有效

$.ajax({
            type: "POST",
        // the url to the service - 
        url: "url",
        // the format that the data should be in when
        // it is returned
        contentType: "json",
            data: '{"Id": "3"}',
        // the function that executes when the server
        // responds to this ajax request successfully
        success: function(data) {

        // put the JSON response in the employees table

        }
根据默认内容类型,默认内容类型为“application/x-www-form-urlencoded”。发送JSON时,内容类型应为“application/JSON;字符集=utf-8',除了WCF不喜欢那样。我收到了相同的错误消息,当我删除内容类型时,我不再收到此错误。顺便说一句,我注意到您将crossDomain设置为true,另一个与该选项相关