question:
2jquery-1.11.0.min.js:4 OPTIONS http://202.115.195.241:8068/Service1.svc/hello1 405 (Method Not Allowed)
http://202.115.195.241:8068/Service1.svc/hello1: Response for preflight has invalid HTTP status code 405.
- WCF code and WebConfig configuration:
[ServiceContract]
public interface IService1
{
[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "hello/{nie}")]
string DoWork(string nie);
[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "hello1")]
string DoWork1();
}
//
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string DoWork(string nie)
{
return jss.Serialize("hello" + nie);
}
public string DoWork1()
{
return jss.Serialize("hello");
}
}
// Webconfig
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!---->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<!--Content-Type,-->
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<security>
<requestFiltering>
<requestLimits maxQueryString="2147483640" maxUrl="2097151" maxAllowedContentLength="2097151"/>
</requestFiltering>
</security>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MapConfigBehavior">
<!-- false -->
<serviceMetadata httpGetEnabled="true"/>
<!-- true false -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindConfig" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxReceivedMessageSize="104857600">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="WCF_POST.Service1" behaviorConfiguration="MapConfigBehavior">
<endpoint address="" binding="webHttpBinding" contract="WCF_POST.IService1" bindingConfiguration="webHttpBindConfig" behaviorConfiguration="webHttp"/>
</service>
</services>
</system.serviceModel>
</configuration>
JQ access WCF code:
function bb() {
$.ajax
({
type: "POST",
url: "http://xxx/Service1.svc/hello",
contentType: "application/json",
data: "{"nie":"aa"}",
timeout: 10000,
dataType: "json",
success: function (response) {
alert(response)
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(thrownError);
}
});
}
$.ajax
({
type: "POST",
url: "http://XXX/Service1.svc/hello",
contentType: "application/json",
data: JSON.stringify({ "nie": "aa"}),
timeout: 10000,
dataType: "json",
success: function (response) {
alert(response)
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(thrownError);
}
});