0

I want to call an API from an html page with angularjs and I have written this script code in html page

$http({
    method:'POST',
    url:'http:192.200.2.2/example.com/api/value',
    headers:{
        X-EXAMPLE.COM-WEB-APP-API-KEY:"ghdhfghgfhdfdf76878678687567",
        X-EXAMPLE.COM-WEB-APP-API-SECRET:"hfghdfghdhfghdf76878678687567",
        Content-Type:"application/json"
     }
 });

I have written in web.config of api,

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="*" />
                <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
           </customHeaders>
       </httpProtocol>
       <security>
           <requestFiltering>
               <verbs>
                   <add verb="OPTIONS" allowed="true" />
               </verbs>
           </requestFiltering>
       </security>
 </system.webServer>

without headers it is calling an api but with header it is not calling an api & I can't use any backend code

  • https://stackoverflow.com/questions/26649361/options-405-method-not-allowed-web-api-2 – Quentin Dec 20 '17 at 12:15
  • Note that this most certainly a server issue, not a client-side issue. Please adjust your post’s tags to reflect the technologies used server-side. – jcaron Dec 20 '17 at 12:20
  • This whole code is working without header but when header with content-type: application/Json is passed it will give above error – neelam mistry Dec 20 '17 at 14:15
  • headers:{ X-EXAMPLE.COM-WEB-APP-API-KEY:"ghdhfghgfhdfdf76878678687567", X-EXAMPLE.COM-WEB-APP-API-SECRET:"hfghdfghdhfghdf76878678687567", content-type: application/json } – neelam mistry Dec 20 '17 at 14:30

1 Answers1

0

Since the headers' name contain symbol (dot and hyphen), try encapsulate them in "" so that they act like a single string.

$http({
    method:'POST',
    url:'http:192.200.2.2/example.com/api/value',
    headers:{
        "X-EXAMPLE.COM-WEB-APP-API-KEY": "ghdhfghgfhdfdf76878678687567",
        "X-EXAMPLE.COM-WEB-APP-API-SECRET": "hfghdfghdhfghdf76878678687567",
        "Content-Type": "application/json"
     }
 });
Popoi Menenet
  • 1,018
  • 9
  • 20