我正在尝试通过 javascript 向 WCF Web 服务方法发出 ajax GET 请求。该请求每次都返回 400“错误请求”错误。但是,如果我通过 WCF 测试客户端调用相同的 Web 服务方法,它会返回正确的结果。
此外,通过 javascript 的 ajax 调用在 GET 和 POST 请求上都返回错误请求错误。
我已将 [WebGet] 属性添加到我的 Web 服务方法,但错误仍然存在。
这是我的 javascript 方法和 ajax 调用:
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”
知道为什么请求在通过 javascript 调用时会返回“错误请求”错误,并且在通过 WCF 测试客户端调用时可以正常工作吗?任何输入将不胜感激!谢谢!
此外,我已经比较了从 WCF 测试客户端发出的请求和使用 Fiddler 从我的 AJAX 请求发出的请求,这是我看到的:
WCF 测试客户端请求:
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)
主机:主机名:端口号连接:保持活动状态
所以我看到的最大区别是 WCF 测试客户端发出 POST 请求并在 SOAP 信封中发送 Web 服务方法名称,而我的 ajax 请求是 GET 并将 Web 服务方法名称添加到 url。查看这些请求,关于如何更改我的 ajax 调用以使其正常工作的任何想法?谢谢!
编辑以添加我的 web.config 文件内容:
<?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>