【问题标题】:ASP MVC ajax POST string controller variable is nullASP MVC ajax POST 字符串控制器变量为空
【发布时间】:2017-11-12 17:24:21
【问题描述】:

Jquery 和控制器代码如下

JQuery:

$.ajax({
         url: "/api/ComicApi",
         datatype: "text",
         data: encoded, //encoded is a MIME64 string    
         type: "POST",
         error: function (request, status, error) {
                  alert('error');
                },
         success: function (data) {

                }
});

ComicApiController.cs

 // POST api/<controller>
 [HttpPost] // tried with [HttpPost] and without
 public void Post([FromBody]string data)
 {
   // I hit my breakpoint, but data is ALWAYS null
 }

【问题讨论】:

  • 你为什么不用 /api/ComicApi/Post url
  • 回答有用吗?
  • @hasan power out -- 这几天我不能测试它,我会告诉你的

标签: ajax asp.net-mvc asp.net-web-api


【解决方案1】:

你能用[HttpPost]试试这个吗

$.ajax({
       type: 'POST',
       url:  "/api/ComicApi",
       data: JSON.stringify(encoded),
       contentType: 'application/json; charset=utf-8',
       dataType: 'json'
       error: function (request, status, error) {
                 alert('error');
              },
       success: function (data) {
                 console.log("success");
              }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    相关资源
    最近更新 更多