Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  168] [ 4]  / answers: 1 / hits: 68969  / 9 Years ago, wed, november 4, 2015, 12:00:00

I have read many similar problems in StackOverflow, but the solutions doesn't work for me.



I have WCF REST service:



[<OperationContract>]    
[<WebInvoke(UriTemplate = PostItem,
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = POST)>]


I can use it using Postman (Chrome extension). I am passing data as 'raw', not 'urlencoded'. And I get 200 return code.



enter



I need to call this method using angularjs:



    $http.post('http://192.168.1.65/Service1.svc/restapi/PostItem',                   
{
Address: г. Москва, ул. Соколово-Мещерская, д.25,
...
User:
})


I have just copied URL and JSON from Postman. But I get the error:




angular.js:10722 OPTIONS
http://192.168.1.65/Service1.svc/restapi/PostItem
http://192.168.1.65/Service1.svc/restapi/PostItem. Response for
preflight has invalid HTTP status code 405




I have searched similar problems and have found two solutions:




  1. Use jQuery to set header Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', but it doesn't work with my WCF service

  2. Set custom headers in my Web.Config:



    <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>



It doesn't help me. And I am not sure that the reason of the error on the server side. The Postman extension can call this method succesfully.



How can I make the same POST call using AngularJS ?



Update:



Here is OPTIONS request:



enter



Review and Response tabs are empty



Update 2:



All works fine in IE, but doesn't work in Chrome.


More From » angularjs

 Answers
21

Looks like I have found solution. I just added second method:



[<OperationContract>]    
[<WebInvoke(UriTemplate = PostTest,
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = POST)>]
abstract PostTest: obj: Test -> unit

[<OperationContract>]
[<WebInvoke(UriTemplate = PostTest,
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = OPTIONS)>]
abstract PostTestOptions: unit -> unit


It is just empty methods that do nothing. I don't know the reason, but all is working.


[#64514] Monday, November 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shawn

Total Points: 507
Total Questions: 103
Total Answers: 111

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
shawn questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Tue, Jun 8, 21, 00:00, 3 Years ago
Sat, Feb 27, 21, 00:00, 3 Years ago
Sat, Dec 19, 20, 00:00, 4 Years ago
Sat, May 9, 20, 00:00, 4 Years ago
;