【问题标题】:POST without URL parameter: 405 (Method Not Allowed)不带 URL 参数的 POST:405(不允许方法)
【发布时间】:2016-07-25 23:16:24
【问题描述】:

我的问题是 405(方法不允许)但我没有解决我的问题 问题出在哪里?为什么没有参数我不能发帖?

javascript代码在这里

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

全局asax就在这里

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");

    }
}

web api 在这里

 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);
    }
}

Chrome 控制台就在这里 Chrome Console

【问题讨论】:

标签: jquery post asp.net-web-api http-status-code-405


【解决方案1】:

使用 post 参数有一些注意事项。

这是我找到的最好的资源:

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

最有可能:

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

还有这种奇怪:

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

【讨论】:

  • 这种怪事太可笑了''='aaa'。如果我发布 name='asd' 值为 null 但我发布为 '' = 'aaa' 它正在工作。
  • 我猜这可能是相关的原始数据类型,因为如果我作为模型发布,我可以获取值。
猜你喜欢
  • 2014-05-23
  • 2018-12-20
  • 2015-11-16
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2013-10-01
相关资源
最近更新 更多