WCF Web服务返回“错误请求”通过Javascript调用时出错 [英] WCF Web Service returns "Bad Request" error when invoked through Javascript

查看:60
本文介绍了WCF Web服务返回“错误请求”通过Javascript调用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过javascript对WCF Web服务方法发出ajax GET请求。请求每次都返回400错误请求错误。但是,如果我通过WCF测试客户端调用相同的Web服务方法,它将返回正确的结果。

I am trying to make an ajax GET request to a WCF web service method through javascript. The request returns with a 400 "Bad Request" error everytime. But if I invoke the same web service method through the WCF Test Client, it returns the correct results.

此外,通过javascript调用ajax会在GET和POST请求中返回错误的请求错误。

Also, the ajax call through javascript returns the bad request error on both GET and POST requests.

I已将[WebGet]属性添加到我的Web服务方法,但错误仍然存​​在。

I have added the [WebGet] attribute to my web service method but the error still persists.

这是我的javascript方法和ajax调用:

Here is my javascript method and ajax call:

function Getdata()
{
var methodUrl = serverUrl + "/GetData";
      $.ajax({
    async: false,
                 type: "GET",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: methodUrl,
    beforeSend: function (XMLHttpRequest) {
    //ensures the results will be returned as JSON.
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    success: function (data) {

    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert("ERROR: GetData() Check your browsers javascript console for more details." + " \n XmlHttpRequest: " + XmlHttpRequest + " \n textStatus: " + textStatus + " \n errorThrown: " + errorThrown);
    }
});
}

这里,serverUrl的格式为:http:// hostname:portnumber /webServiceProxy.svc

Here, serverUrl is of the format: "http://hostname:portnumber/webServiceProxy.svc"

任何想法为什么请求在通过javascript调用时会返回错误请求错误并在通过WCF测试客户端调用时完美地工作?任何输入将不胜感激!谢谢!

Any idea why the request would return a "Bad Request" error when invoked through javascript and work perfectly when invoked through the WCF Test Client? Any inputs will be appreciated! Thank you!

另外,我已经比较了从WCF测试客户端发出的请求以及使用Fiddler发出的AJAX请求,这就是我所看到的:

Also, I have compared the requests sent out from the WCF Test Client and from my AJAX request using Fiddler and here is what I see:

WCF测试客户端请求:

WCF Test Client Request:

POST http://hostname:portnumber/WebServiceProxy.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IWebServiceProxy/GetData"
Host: hostname:portnumber
Content-Length: 144
Expect: 100-continue
Accept-Encoding: gzip, deflate

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetData xmlns="http://tempuri.org/"/></s:Body></s:Envelope>

我的AJAX请求:

GET http://hostname:portnumber/WebServiceProxy.svc/GetData HTTP/1.1
Content-Type: application/json; charset=utf-8
Accept-Language: en-us
Referer: http://hostname/OrgName/WebResources/resourceName?pagemode=iframe
Accept: application/json
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ENUS)

主机:主机名:portnumber
连接:Keep-Alive

Host: hostname:portnumber Connection: Keep-Alive

所以我看到的最大区别是WCF测试客户端发出POST请求当我的ajax请求是GET并将web服务方法名称添加到url时,将Web服务方法名称发送到SOAP信封中。看看这些请求,关于如何更改我的ajax调用以使其正常工作的任何想法?谢谢!

So the biggest difference that I see is that the WCF Test Client makes a POST request and sends the web service method name in a SOAP envelope while my ajax request is a GET and adds the web service method name to the url. Looking at these requests, any ideas on how I might have to change my ajax call to get it to work correctly? Thanks!

编辑添加我的web.config文件内容:

Editing to add my web.config file contents:

<?xml version="1.0"?>

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off" />
</system.web>

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IDDSWCFService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://machinename:8080/DDSWCFService" binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IDDSWCFService" contract="WCFService.IDDSWCFService"
            name="WSHttpBinding_IDDSWCFService">
            <identity>

            </identity>
        </endpoint>
    </client>
    <behaviors>
        <serviceBehaviors>

            <behavior>
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>

        </serviceBehaviors>

        <endpointBehaviors>
            <behavior name="webHttpBehavior">
                <enableWebScript/>
                <webHttp helpEnabled="true"/>
            </behavior>
        </endpointBehaviors>

    </behaviors>
</system.serviceModel>

推荐答案

感谢所有回复。令人惊讶的是,我设法修复了错误,但解决方案完全不是我所期待的。我必须在Service1.svc文件的标记中添加一个额外的属性:

Thanks for all the responses. Surprisingly enough, I managed to fix the error, but the solution was totally not what I was expecting. I had to add an additional attribute to the markup of the Service1.svc file:

Factory =System.ServiceModel.Activation.WebScriptServiceHostFactory

Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory

我不确定为什么在构建项目时没有自动添加到文件中,但那就是它做了什么。我现在能够通过浏览器以及通过我调用Test方法AJAX请求没问题。

I am not sure why this wasn't automatically added to the file when the project was built, but thats what did it. I am now able to invoke the Test method through the browser as well as through my AJAX request with no problem.

再次感谢大家的帮助。

这篇关于WCF Web服务返回“错误请求”通过Javascript调用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆