Error consuming a REST API service with the POST method using AngularJS

2

I am consuming a REST API service with angularJS and the POST method, but when consuming the service, it sends me a request with the OPTIONS method. I do not understand why? I have read and leave in the web.config of the site the following lines:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Credentials" value="true"/>
      <add name="Access-Control-Allow-Origin" value="https://www.zonapagos.com" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>
  <validation validateIntegratedModeConfiguration="false" />
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <!--<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />-->
    <remove name="WebDAV" />
  </handlers>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule" />
  </modules>
  </system.webServer>

but I still send first the request OPTIONS

It has something to do with CORS but I can not find how to solve it

    
asked by Ivan Dario Duque M. 07.06.2017 в 22:36
source

1 answer

0

there is a common error with angular integrating it with Java or .NET, you have two options the first you must add the line in the app.js

.config(function(...., $httpProvider  ) {

  $httpProvider.defaults.headers.post = {};
    $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

the second one: modify each function of your http

$http({
    url: "tu url",
    method: "POST",
   params: {
      valor1: 'valor1',
      valor2: 'valor2'
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
});
    
answered by 07.06.2017 / 23:16
source