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.
Also, the ajax call through javascript returns the bad request error on both GET and POST requests.
I have added the [WebGet] attribute to my web service method but the error still persists.
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);
}
});
}
Here, serverUrl is of the format: “http://hostname:portnumber/webServiceProxy.svc”
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!
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 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>
My AJAX Request:
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)
Host: hostname:portnumber
Connection: Keep-Alive
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!
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>
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
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.
Thanks again for everybody’s help.