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
The 405 Method Not Allowed error occurs when the web server is configured in a way that does not allow you to perform a specific action for a particular URL. It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the target resource.
The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method. The server must generate an Allow header field in a 405 status code response.
If you are certain you need a POST request, chances are, you're just using the wrong endpoint on the server. Again, this can only be solved if you have proper documentation to show what methods are supported for each endpoint.
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: https://stackoverflow.com/a/27374066/4304188
Solution:
$http({
url: "",
method: "POST",
data: $.param({
data1: ""
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
});
I was also facing this issue. Was getting 405 and OPTIONS was made. I came across a blog and found that the way angular encodes param is not understood by web server properly. So, jQuery is required to encode POST data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With