Вызов службы WCF с использованием jquery Ajax завершился неудачно с неверным ответом на запрос - PullRequest
0 голосов
/ 14 июля 2020

вызов веб-службы wcf Использование jquery - Ajax не удалось из-за неверного запроса, не зная точной проблемы. он даже не вызывает никаких методов успешным или неудачным. и веб-служба, и веб-сайт развертываются на одном сервере с использованием 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:

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

готов предоставить вам более подробную информацию.

1 Ответ

1 голос
/ 15 июля 2020

Ниже 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}
...