rosh_021 rosh_021 - 18 days ago 9
Ajax Question

calling wcf service from jquery ajax - getting exception - incoming request is not allowed

Exception details:


A first chance exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Web.dll
Additional information: The HTTP method 'GET' of the incoming request (with URI http://localhost:24342/Test.svc/DoWork?callback=jQuery110203769165082986856_1479380612393&testParameter=TEST&_=1479380612394) is not allowed.


I am using Ajax-embedded WCF service. WCF client is a jQuery Ajax call. My code:

[ServiceContract(Namespace = "testWCFAjax")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )]
public string DoWork(string testParameter)
{
return "success";
}
}


Web Config:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="Service">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service"/>
</service>
</services>
</system.serviceModel>


Ajax Code:

<script src="Scripts/jquery-1.9.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallMyService() {
$.support.cors = true;
var param = '{"testParameter": "TEST"}';
$.ajax({
url: 'http://localhost:24342/Test.svc/DoWork',
data: param,
type: "POST",
dataType: "jsonp",
contentType: "application/json",
success: ServiceSucceeded,
error: ServiceFailed
});
}

Answer

I was able to solve this issue by creating WCF service with Ajax-embedded.