AngularJS $ http.POST方法405错误 [英] AngularJS $http.POST method 405 error
问题描述
在$ http.post方法我得到405问题。我们使用单一服务(REST)用于POST& GET方法。
如果url有localhost它正在工作。 urlAddScenario是localhost / Service.svc / api / scenarios / add。如果我使用机器名而不是locahost它不工作。 machinname / Service.svc / api / scenarios / add
我的JS代码是
scenarioExecutionFactory.addScenario = function(scenarioId){
return $ http.post(urlAddScenario,scenarioId)
};
anotherJS:
code> var runScenarioId = {ScenarioId:10}
scenarioExecutionFactory.addScenario(runScenarioId)
.success(function(data){
$ scope.getScenarioRecentRuns($ scope.CurrentScenario );
});
WCF服务:
[OperationContract]
[WebInvoke(UriTemplate =api / scenarios / add,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method =POST)]
请求AddScenario(ScenarioRequestParams requestParams);
config:
< system.webServer>
< httpProtocol>
< customHeaders>
< add name =Access-Control-Allow-Originvalue =*/>
< add name =Access-Control-Allow-Credentialsvalue =true/>
< add name =Access-Control-Allow-Methodsvalue =GET,POST,PUT,OPTIONS/>
< add name =Access-Control-Allow-Headersvalue =Origin,Content-Type,Accept,Authorization,X-Ellucian-Media-Type/>
< / customHeaders>
< / httpProtocol>
**选项* * http://ddd/HelloService/HelloService.svc/PostData HTTP / 1.1
接受:* / *
原产地:http:// localhost:31284
访问控制请求方法:POST
Access-Control-Request-Headers:accept,content-type
Accept-Encoding:gzip,deflate
User-Agent:Mozilla / 5.0(Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0)like Gecko
Host:vijayakatta
Content-Length:0
DNT:1
Connection:Keep-Alive
Pragma:no-缓存
错误:
IE:SCRIPT7002 :XMLHttpRequest:网络错误0x80070005,访问被拒绝。
Chrome:XMLHttpRequest无法加载htp // machinename / services.svc / api / scenario / add。 HTTP状态代码405无效
响应标题:HTTP / 1.1 405方法不允许
为了通过 web.config
正确启用 CORS
,您需要指定这3个自定义标题
< httpProtocol>
< customHeaders>
< add name =Access-Control-Allow-Originvalue =*/>
< add name =Access-Control-Allow-Headersvalue =Content-Type/>
< add name =Access-Control-Allow-Methodsvalue =GET,POST,PUT,DELETE,OPTIONS/>
< / customHeaders>
< / httpProtocol>
如果计划支持 PUT
code> DELETE 动词,您还需要处理 preflight 检查。
在此答案中,您可以阅读有关 CORS
和 preflight检查的更多信息: http://stackoverflow.com/a/27374066/4304188
In $http.post method I'm getting 405 issue. We are using single service(REST) for both POST & GET methods.
if url having localhost it is working. urlAddScenario is localhost/Service.svc/api/scenarios/add. If I'm using machine name instead of locahost it is not working. machinname/Service.svc/api/scenarios/add
My JS code is
scenarioExecutionFactory.addScenario = function (scenarioId) {
return $http.post(urlAddScenario, scenarioId)
};
anotherJS:
var runScenarioId = { "ScenarioId": 10 }
scenarioExecutionFactory.addScenario(runScenarioId )
.success(function (data) {
$scope.getScenarioRecentRuns($scope.CurrentScenario);
});
WCF Service:
[OperationContract]
[WebInvoke(UriTemplate = "api/scenarios/add",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST")]
Request AddScenario(ScenarioRequestParams requestParams);
config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin,Content-Type,Accept,Authorization,X-Ellucian-Media-Type" />
</customHeaders>
</httpProtocol>
I'm seeing OPTIONS instead of POST in Headers.
Headers:
**OPTIONS** http://ddd/HelloService/HelloService.svc/PostData HTTP/1.1
Accept: */*
Origin: http://localhost:31284
Access-Control-Request-Method: POST
Access-Control-Request-Headers: accept, content-type
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
Host: vijayakatta
Content-Length: 0
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
Error:
IE: SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.
Chrome: XMLHttpRequest cannot load htp//machinename/services.svc/api/scenarios/add. Invalid HTTP status code 405
Response Headers: HTTP/1.1 405 Method Not Allowed
In order to enable CORS
via web.config
properly you need to specify these 3 custom headers
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
If you plan to support PUT
and DELETE
verbs you also need to handle the preflight check.
You can read more about CORS
and preflight check in this answer: http://stackoverflow.com/a/27374066/4304188
这篇关于AngularJS $ http.POST方法405错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!