簡體   English   中英

使用 jquery Ajax 調用 WCF 服務失敗,請求響應錯誤

[英]Calling WCF Service Using jquery Ajax failed with bad request response

在不知道確切問題的情況下,調用 wcf web 服務使用 jquery-Ajax 失敗,請求錯誤。 它甚至沒有調用任何方法成功或失敗。 web 服務和網站都使用 iis 部署在同一台服務器上

錯誤:

Failed to load resource: the server responded with a status of 400 (Bad Request)

該代碼用於調用服務方法:

function test(){
     try {

       
        code = getValuesWrittenInTheURL[0] + "";

       
        var query = "select taskstatus,tasksubstatus,Lat,Lng,elementID from tasks_gvt where code = '" + code + "'";
        
        $.ajax({
            type: "POST",
            async: true,
            url: IP + "/GetData", 
            data: JSON.stringify({ Query: query }),
            dataType: "json",
            success: function (data) {
                debugger;
                console.log("data: ",data);
           
            },
            failure: function (errMsg) {
                debugger;
                console.log("err",errMsg);
                
            }
           
        });
    } catch (error) {
        console.log("alaaError", error.message);
    }
    
}

wcf web 服務運營合同:

 [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        Response GetData(String Query);

准備為您提供更多詳細信息。

下面是我成功接入服務的Ajax,可以參考:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ajax</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
$(document).ready(function(){
    $("button").click(function () {
        code = "code";
        var query = "select taskstatus,tasksubstatus,Lat,Lng,elementID from tasks_gvt where code = '" + code + "'";
        da={"Query":query}
        $.ajax({
            type: "Post",
            dataType: "json",
           contentType: "application/json;charset=utf-8",
           data: JSON.stringify(da),
            url: "http://localhost/Service1.svc/GetData", success: function (result) {
        }});
    });
});
    </script>
</head>
<body>


    <button>Call WCF Rest Service</button>

</body>
</html>

你需要設置contentType,我看到你設置了BodyStyle為Wrapped,如果設置為Wrapped,請求格式應該是這樣的:

{"Query":query}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM