Ajax problem with Local vs remote service in .NET WCF
Attached is a Visual Studio 2012 project that contains a solution with two web projects. One contains a WCF Service plus a single web page Default.aspx and the second project is just a single web page also called Default.aspx. The two aspx pages are
identical except a change in namespace on the very first lines.
When I run the solutaion which is using 10.1.2 and load the two web pages the page in the same project as the service can call the service with out problem. But the page that is in the separate project by itself fails with a generic "error" message returned. If I open any browser and paste the service URL it returns the json string as it should. Looking at IIS Express I can see both web apps running so I do not think it is a problem that with the service not being loaded.
Here is the ajax call itself:
$(document).ready(function() { $("#btnNext").click(function () { var request = $.ajax({ url: GetJsonObject, type: "GET", cache: false, dataType: "json" }); request.done(function (jsonobject) { $("#spanMain").html("").append(jsonobject); }); request.fail(function (jqXHR, textStatus) { alert("Request failed: " + textStatus); }); }); })
I also tried setting the crossDomain to true but that made no difference.
The service method it is calling is this:
[ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class ServiceAdventureWorks { [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Json)] public string TestMethod() { return "Test String Worked!"; } }
And the web.config file for the Service web site looks like this in the service config section:
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="ServiceAdventureWorks.EnpointBehavior" > <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="ServiceAdventureWorks.ServiceBehavior"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="ServiceAdventureWorks.ServiceAdventureWorks" behaviorConfiguration="ServiceAdventureWorks.ServiceBehavior"> <endpoint address="" behaviorConfiguration="ServiceAdventureWorks.EnpointBehavior" binding="webHttpBinding" contract="ServiceAdventureWorks.ServiceAdventureWorks" /> </service> </services> </system.serviceModel>
Hope that all makes sense. If anyone can explain why it works when the web service is local but not remotely it would be appreciated.
Thanks
Ernie