Search code examples
jquerypostasp.net-web-apihttp-status-code-405

POST without URL parameter: 405 (Method Not Allowed)


My problem is 405 (Method not allowed) but i did not solve my problem where is the problem? Why can not i doing post without parameters?

javascript code is here

 <script type="text/javascript">
    $(document).ready(function () {
        var getResult = GetData();
        console.log("GetData => " + getResult);
        var getResult2 = PostDataURLParamater();
        console.log("PostDataURLParamater => " + getResult2);
        var getResult3 = PostData();
        console.log("PostData => " + getResult3);
    })
    function GetData() {
        var result;
        $.ajax({
            url: "http://localhost:64073/api/home/helloworld",
            type: "GET",
            datatype: "JSON",
            async: false,
            success: function (data) {
                result = data;
            },
            error: function (msg) {
                console.log("Error: " + msg);
            }
        });
        return result;
    };
    function PostDataURLParamater() {
        var result;
        $.ajax({
            url: "http://localhost:64073/api/home/HelloWorldForPost?name=MustafaParamater",
            type: "POST",
            datatype: "JSON",
            async: false,
            success: function (data) {
                result = data;
            },
            error: function (msg) {
                console.log("Error: " + msg);
            }
        });
        return result;
    }
    function PostData() {
        var result;
        var data = JSON.stringify({ name: "Mustafa" });
        $.ajax({
            type: 'POST',
            url: 'http://localhost:64073/api/home/HelloWorldForPost',
            data: data,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: false,
            success: function (data) {
                result = data;
            },
            error: function (msg) {
                console.log("Error: ", msg);
            }
        });
        return result;
    }
</script>

and global asax is here

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");

    }
}

and web api is here

 public class HomeController : ApiController
{
    public const string fv = "Hello Wold!";
    [HttpGet]
    public string HelloWorld()
    {
        return fv;
    }


    [HttpPost]
    public string HelloWorldForPost(string name)
    {
        return string.IsNullOrEmpty(name) ? fv : (fv + " by " + name);
    }
}

and chrome console is here Chrome Console


Solution

  • There are caveats to using post parameters.

    Here is the best resource I have found:

    http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

    Most likely:

    // POST api/values
    public string Post([FromBody]string value) {
      return value;
    }
    

    and this weirdness:

    $.post('api/values', { '': value });