1

I'm new with angular and i try to consume a post method of a WCF. For this i use HttpClient from '@angular/common/http'

My rest service has this sign

[OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "SaveUser")]
    String SaveUser(User user);

My user class is really simple with firstname, lastname, ... and in my SaveUser method i register user in DB.

Now in my ionic project i have this in my provider but i receive: 405 (Method Not Allowed) If i pass JSON.stringify(data) instead of data i get back an error 400 bad request.

console.log(JSON.stringify(data));
var url = this.apiURL + "/SaveUser";
return new Promise((resolve, reject) => {
  this.http.post(url, data)
    .subscribe(res => {
      resolve(res);
      console.log(res);
    }, (err) => {
      console.log(err);
      reject(err);
    });
});

For info here is a console.log of data

{"City":"Liege","Email":"test@gmail.com","Firstname":"florine","IsAdmin":false,"Lastname":"detry","Mobile":"0498/123456","Password":"1234","Phone":"","Street":"avenue jean 7","UserID":2,"Zipcode":"4000"}

I added chrome extension: Allow-Control-Allow-Origin: * and get response works correctly

I show you a printscreen from the console in Chrome when i try a post. enter image description here

user1898765
  • 323
  • 1
  • 6
  • 18
  • 1
    Is your WCF service running on a different domain or port to the front end HTTP server? At a guess, it is and your app is sending a pre-flight `OPTIONS` request and you haven't configured CORS access, hence the _"method not allowed"_. See https://www.codeproject.com/Articles/845474/Enabling-CORS-in-WCF – Phil Dec 26 '17 at 22:47
  • Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – Phil Dec 26 '17 at 22:50
  • Next time, please include the full error response text. It should have been something like _"OPTIONS `https://www.example.com/wcfv1/service1.svc/TestMethod` test1.html:1 XMLHttpRequest cannot load `https://www.example1.com/wcfv1/service1.svc/TestMethod`. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin `'http://localhost'` is therefore not allowed access. The response had HTTP status code 405."_ – Phil Dec 26 '17 at 22:52
  • Thanks for your help. i completed the post with log from chrome console – user1898765 Dec 27 '17 at 08:23
  • i added a global.asax in my wcf like you propose in first comment and now it works. Thanks a lot – user1898765 Dec 27 '17 at 08:55

0 Answers0