ASP MVC ajax POST字符串控制器变量为null

时间:2017-06-10 23:27:30

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

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
 }

1 个答案:

答案 0 :(得分:0)

你能用[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");
              }
});