Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http.POST method 405 error

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

like image 482
user3194721 Avatar asked Dec 09 '14 19:12

user3194721


People also ask

What is 405 error in Angular?

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.

What is HTTP status 405 method not Allowed?

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.

How to fix 405 method not Allowed in POSTman?

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.


2 Answers

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

like image 123
Mihai Dinculescu Avatar answered Oct 20 '22 00:10

Mihai Dinculescu


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

like image 26
govindc Avatar answered Oct 20 '22 00:10

govindc