问题:
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代码及WebConfig配置:
[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
JQ访问WCF代码:
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);
}
});