【问题标题】:JQuery / MVC / ajax how to post data to controller using ajax?JQuery / MVC / ajax 如何使用 ajax 将数据发布到控制器?
【发布时间】:2016-12-07 21:18:11
【问题描述】:

我正在尝试使用 ajax 传递数据并在我的 mvc 项目中触发控制器

这是我的控制器

public class FileController : Controller
{
    [HttpPost]
    public ActionResult Index(string data)
    {
        return View();
    }
}

这是js

 $('#getmessage').on('click', function () {
        var text = '';
        $('#discussion>li').each(function () {
            text += $(this).text();
            text += '\n'
        })
        console.log(text)

        $.ajax({
            url: 'http://localhost:22828/File/Index',
            type: 'POST',
            data: text,
            success: function (result) {
                console.log("data sended");
            }
        })
    })

我需要将文本传递给控制器​​,但在我的控制器中我得到NULL
有人可以说明一下吗?
提前致谢

【问题讨论】:

    标签: jquery ajax asp.net-mvc


    【解决方案1】:

    您的控制器中缺少[FromBody] 标签。默认情况下,ASP.NET 会尝试从 URL 中绑定简单的参数,并且只从正文中绑定复杂的参数。

    试试这样:

    public class FileController : Controller
    {
        [HttpPost]
        public ActionResult Index([FromBody] string data)
        {
            return View();
        }
    }
    

    更多详情见this相关回答。

    【讨论】:

      【解决方案2】:

      这样通过记录data

      url: 'File/Index?data=' + text, // Just make change here..
      

      您只需像这样更改您传递给控制器​​ActionURL

      $('#getmessage').on('click', function () {
          var text = '';
          $('#discussion>li').each(function () {
              text += $(this).text();
              text += '\n'
          })
          console.log(text)
      
          $.ajax({
              url: 'File/Index?data=' + text, // Just make change here..
              type: 'POST',
              data: text,
              success: function (result) {
                  console.log("data send");
              }
          })
      })
      

      【讨论】:

      • 仍然无法正常工作,在我的控制器中获取 text = null
      • 首先通过将“调试器”放入其中来检查 JavaScript 中的文本。并通过 put 静态值检查...
      【解决方案3】:

      将您的 Javascript 更改为:

      $('#getmessage').on('click', function () {
              var text = '';
              $('#discussion>li').each(function () {
                  text += $(this).text();
                  text += '\n'
              })
              console.log(text)
      
              $.ajax({
                  url: 'http://localhost:22828/File/Index',
                  type: 'POST',
                  data: { data: text }, // This is all you have to change
                  success: function (result) {
                      console.log("data sended");
                  }
              })
          })
      

      【讨论】:

      • 控制器仍然获取数据 = null
      • 当你做console.log(text)时,有什么事吗?另外请记得用; 关闭您的线路,这样每个人都可以更轻松地知道线路的起点和终点。
      • 好的,打开你的 /App_Start/RouteConfig.cs 并将方法的内容粘贴到这里 public static void RegisterRoutes( RouteCollection routes ) please...
      • 它现在可以工作了,我将我的类型更改为 'GET' insted of post,但现在我必须在 url 中发送它,但现在还可以。
      • 好的,太好了...您也可以更改type: 'GET'
      猜你喜欢
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多