0

I am getting this error zone.js:3243 POST https://localhost:44424/api/SlgCorpNotes/Edit 405 (Method Not Allowed)

Here is my service api call

  updateMessage(message: any) {
    console.log("at service")
    console.log(message)
    return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', message)
  }

When I console.log message I receive this.

Object
departments: 4
noteBody: "asdf"
weeks: SLGTime {year: 2020, week: 12, quarter: 1, weekEnding: "2020-01-18T00:00:00", lyweekEnding: "2019-01-19T00:00:00"}
__proto__: Object

So I know I am sending information to the .net controller in the back end. Here is my controller method. Is this possibly not set up to receive the angular info correctly?

[HttpPost]
[Route("api/SlgCorpNotes/Edit")]
public void Edit([FromBody]object item)
{

    _SLGContext.Entry(item).State = EntityState.Modified;
    _SLGContext.SaveChanges();
}

More info that may be useful. Here is the name of my namespace and Route

namespace mocHub2.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SlgCorpNotesController : Controller

This is my formgroup info that i'm sending.

this.optionsForm = new FormGroup({
  departments: new FormControl(),
  weeks: new FormControl(),
  noteBody: new FormControl()
})

I think what it is, is Im not naming something correctly. Sorry this is my first .net/angular project. Usually Im just on razor views and .net mvc!

4
  • Try to see whether your code this.baseUrl + 'api/SlgCorpNotes/Edit' creates a correct URL. It can be seen what url is used to make API call in network tab of your browser Commented Oct 29, 2019 at 19:09
  • Do you have CORS configured to allow an HTTP OPTIONS verb? Commented Oct 29, 2019 at 19:16
  • I will check that StepUp and Jonathon, Im sure I do have Cors, I was able to make api calls for other methods Commented Oct 29, 2019 at 19:39
  • still no luck :( I created a model for the object being sent and received. It is an object with one int, one datetime, and one string. This should be easy right? I am able to do a get post easy! Commented Oct 29, 2019 at 23:53

2 Answers 2

1

Try sending it as a JSON,

return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', JSON.stringify(message));

and your method body should have the object of the type

public void Edit([FromBody]TypeOfObject item)
Sign up to request clarification or add additional context in comments.

3 Comments

It still gives me the error zone.js:3243 POST localhost:44424/api/SlgCorpNotes/Edit 405 (Method Not Allowed)
still no luck :( I created a model for the object being sent and received. It is an object with one int, one datetime, and one string. This should be easy right? I am able to do a get post easy!
0

I created an object manually. newMessage.

I also had to put HttpHeaders

const headers = new HttpHeaders()
  .set('Content-Type', 'application/json;charset=UTF-8')

let options = { headers: headers };

Here is the method updated.

updateMessage(message: any) {
    console.log("at service")
    console.log(message)
    //var newMessage = new CorpNotes(message['departments'], message['noteBody'], message['weeks'].weekEnding)
    //console.log(newMessage)
    //console.log(JSON.stringify(newMessage))
    var newMessage = {
      "Departments": message["departments"],
      "Note": message["noteBody"],
      "WeekEnding": message["weeks"].weekEnding
    }
    console.log(newMessage)
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json;charset=UTF-8')

    let options = { headers: headers };

    return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit',newMessage, options);
    //return this.http.post(this.baseUrl + 'api/SlgCorpNotes/Edit', JSON.stringify(newMessage))


  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.