我正在尝试完整的Angularjs网站进行培训。我已经阅读了许多关于此方法不允许的文章,但没有找到解决方案。我试图将数据对象发送到我的服务。
错误:无法加载资源:服务器响应状态为405(方法不允许)
这是我的AngularJS Part。
var addNews =
{
Title: newsList.newsTitle,
NewsContent: newsList.newsContent,
CreationDate: newsList.newsCreationDate,
CreatedBy: newsList.newsAuthor,
ModificationDate: newsList.newsCreationDate,
ModifiedBy: newsList.newsAuthor
};
var news = JSON.stringify(addNews);
$http({
method: 'POST',
dataType: 'json',
url: 'http://localhost:11672/InfinytecServices.svc/SaveNews',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: news
});
这是我的服务部分
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/SaveNews",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
int SaveNews(News news);
WebConfig Comming
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<extensions>
<behaviorExtensions>
<add
name="crossOriginResourceSharingBehavior"
type="InfinytecWebService.CORSEnablingBehavior, InfinytecWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
<crossOriginResourceSharingBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="serviceBehavior" name="InfinytecWebService.InfinytecServices">
<endpoint address=""
behaviorConfiguration="web"
binding="webHttpBinding"
contract="InfinytecWebService.IInfinytecServices" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
至少,CORS
public class CORSEnablingBehavior : BehaviorExtensionElement, IEndpointBehavior
{
public void AddBindingParameters(
ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters){ }
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new CORSHeaderInjectingMessageInspector()
);
}
public void Validate(ServiceEndpoint endpoint) { }
public override Type BehaviorType { get { return typeof(CORSEnablingBehavior); } }
protected override object CreateBehavior() { return new CORSEnablingBehavior(); }
private class CORSHeaderInjectingMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(
ref Message request,
IClientChannel channel,
InstanceContext instanceContext)
{
return null;
}
private static IDictionary<string, string> _headersToInject = new Dictionary<string, string>
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS" },
{ "Access-Control-Allow-Headers", "X-Requested-With,Content-Type" }
};
public void BeforeSendReply(ref Message reply, object correlationState)
{
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in _headersToInject)
httpHeader.Headers.Add(item.Key, item.Value);
}
}
}
你能帮帮我吗?
事先,谢谢! :)
答案 0 :(得分:0)
尝试以下其中一项
浏览器会使用两个请求来获取使用选项的转机前信息,另一个请求用于实际的api呼叫。
您遇到的问题是浏览器第一次调用您的端点正在拒绝Method = OPTIONS。因为它只能处理POST。