繁体   English   中英

jQuery Ajax发布到WCF,返回“错误请求”

[英]jQuery Ajax Post to WCF returning “Bad Request”

我似乎不太明白这一点。

我有这样的WCF服务;

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    bool Test();

然后执行;

    public bool Test()
    {
        return true;
    }

然后是我的jQuery;

                jQuery.support.cors = true;
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json",
                    url: "http://localhost:8732/Design_Time_Addresses/MyService/Service/mex/Test",
                    timeout: 2000,
                    success: function (data) {
                        alert(9);
                    },
                    error: function (xhr, status, error) {
                        alert(status);
                        alert(error);
                    }
                });

每次我称此为“错误请求”。 如果我将URL更改为此,请说;

http://localhost:8732/Design_Time_Addresses/MyService/Service/mex/Tesdt

我收到错误“未知”,因此我认为它正在寻找我的服务。

您的服务器方法中有ResponseFormat = WebMessageFormat.Json

但是在您的AJAX调用中, dataType (您希望从服务器返回)为text

TLDR:

更改dataType: "text" ----> dataType: "json"

通常,除非将Web端点地址配置为该地址,否则通常不应将请求发送到“ mex”端点。 如果您的webHttpBinding / webHttpBehavior端点位于http:// localhost:8732 / Design_Time_Addresses / MyService / Service ,则服务中的URI应该就是这样。

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: "http://localhost:8732/Design_Time_Addresses/MyService/Service/Test",
    timeout: 2000,
    success: function (data) {
        alert(9);
    },
    error: function (xhr, status, error) {
        alert(status);
        alert(error);
    }
});

检查该地址,如果该地址不起作用,请发布您的web.config和global.asax.cs(如果要定义任何路由)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM