簡體   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