i know it's looks like duplicate question.
Here i want to call my wcf rest service using jquery ajax and want to pass country object which have countryname as parameter in rest service. but whenever I am calling my rest service using jquery ajax from html page.but it is giving me error 405 method not allowed
.I tried lots of time to solve it.but i am enable to solve this error.And i am trying to pass data in json object.
Iservice.cs:
[OperationContract]
[WebInvoke(UriTemplate = "/AddCountry", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
String AddCountry(tableCountry Country);
Service.cs
public string AddCountry(tableCountry Country)
{
//do Code.
}
Web.config
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webbehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service">
<endpoint address="rest" binding="webHttpBinding" contract="IService" bindingConfiguration="webHttpBinding" behaviorConfiguration="webbehaviour"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
Ajax Code
$("document").ready(function(){
var country = {"CountryName":"Iran"};
$.ajax({
type: "POST",
url: "http://localhost:2293/ACFRestAjaxParsing/Service.svc/rest/AddCountry",
data: JSON.stringify({ Country: country }),
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
});
I refer this link to solve this error Stackoverflow
If anyone have idea about this error in my case then please help me.
Thanks in advance.