I am sending post method to server but receiving get method

I am working on .net core 3.1 api

I got error when calling api. Server responce 405

jquery.min.js:2 GET https://localhost:44345/api/Home/AddUpdateUser?callback=jQuery351026836393378483203_1613635815143&name=Ahsan&email=ahsan%40gmail.com&password=asdsdasda&_=1613635815145 net::ERR_ABORTED 405

This is my api controller class

public class HomeController : ControllerBase
    {
        private IRegisterUser _user;
        private IMapper _mapper;

        public HomeController(IRegisterUser user, IMapper mapper)
        {
            _user = user;
            _mapper = mapper;
        }

        //api/Home
        [HttpGet]
        public IActionResult GetAllUser()
        {
            var users = _user.GetAllUser();
            if (users != null)
            {
                var userVm = _mapper.Map<List<RegisterUserVm>>(users);
                return Ok(userVm);
            }

            return Ok("Some error occured");
        }

        //api/Home
        [HttpPost]
        [Route("[action]")]
        [Route("api/Home/AddUpdateUser")]
        public IActionResult AddUpdateUser([FromBody] RegisterUserVm userVm)
        { 
            //userVm.MacAddress = GetMacAddress();
            var user = _mapper.Map<User>(userVm);
            var msg = _user.AddUser(user);
            string[] message = { "200", "testing" };
            return Ok(message);
        }

        //api/Home/{id}
        [HttpDelete]
        public IActionResult DeleteUser(Guid Id)
        {
            var msg = _user.DeleteUser(Id);
            return Ok(msg);
        }

        [HttpGet]
        [Route("[action]")]
        [Route("api/Home/GetUserById")]
        public IActionResult GetUserById(Guid Id)
        {
            var user = _user.GetUserById(Id);
            var userVm = _mapper.Map<RegisterUserVm>(user);
            if (userVm != null)
                return Ok(userVm);
            return Ok("Some error occured");
        }

        
    }

Here is my ajax call

<script>
    $("#button").click(function (e) {
        e.preventDefault()
        var data = {
            name: "Ahsan",
            email: "ahsan@gmail.com",
            password: "asdsdasda"
        };
        jQuery.support.cors = true;
        $.ajax({
            type: "POST",
            dataType: 'jsonp',
            url: "https://localhost:44345/api/Home/AddUpdateUser",
            data: data,
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                debugger;
                alert('ok');
            },
            error: function (result) {
                debugger;
                alert('error');
            }
        });
    });
</script>

4 thoughts on “I am sending post method to server but receiving get method”

  1. Try to change your ajax to

    $.ajax({
                type: "POST",
                dataType: 'json',
                url: "https://localhost:44345/api/Home/AddUpdateUser",
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    debugger;
                    alert('ok');
                },
                error: function (result) {
                    debugger;
                    alert('error');
                }
            });
    

    You need to change dataType to json as Rena said,and as your contenttype is application/json; charset=utf-8,you need to change data to json type with JSON.stringify(data).

    Reply

Leave a Comment