简体   繁体   English

使用 jquery Ajax 调用 WCF 服务失败,请求响应错误

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

calling wcf web service Using jquery-Ajax failed with bad request without knowing the exact problem.在不知道确切问题的情况下,调用 wcf web 服务使用 jquery-Ajax 失败,请求错误。 it's not even calling any methods success or failure.它甚至没有调用任何方法成功或失败。 both the web service and the website is deployed on the same server using iis web 服务和网站都使用 iis 部署在同一台服务器上

the error:错误:

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

the code is used to call the service methods:该代码用于调用服务方法:

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);
    }
    
}

the wcf web service Operation contract: wcf web 服务运营合同:

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

ready to provide you with any more details.准备为您提供更多详细信息。

Below is the Ajax I successfully accessed the service, you can refer to it:下面是我成功接入服务的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>

You need to set contentType, and I see that you set BodyStyle to Wrapped,if it is set to Wrapped, the request format should be like this:你需要设置contentType,我看到你设置了BodyStyle为Wrapped,如果设置为Wrapped,请求格式应该是这样的:

{"Query":query}

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

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