【问题标题】:Why is my ajax not posting to my controller?为什么我的 ajax 没有发布到我的控制器?
【发布时间】:2014-03-23 03:58:58
【问题描述】:

我正在尝试将数据从掌上电脑发布到我的控制器。该表加载正常并且工作正常,但是当我单击保存按钮时没有任何反应。然后我做了一个 F12 并看到一个错误:

请求的 URL:/Scripts/jquery.min.map

经过研究,我找到了这个链接...... jquery 1.9.0 and modernizr cannot be minified with the ASP.NET Web Optimization Framework ……但它并没有解决我的问题。该操作不会被调用,并且错误消息是相同的。

可能是什么问题。请帮助我了解发生了什么。谢谢。

 <script src="../../Scripts/jquery.min.js" type="text/javascript"></script>
 <script src="../../Scripts/jquery-ui.custom.js" type="text/javascript"></script>

 <script src="../../Scripts/jquery.handsontable.full.js" type="text/javascript"></script>

 <script src="../../Scripts/numeral.sv-se.js" type="text/javascript"></script>

 <script src="../../Scripts/jquery.handsontable-excel.js" type="text/javascript"></script>


 <link href="../../Content/themes/base/jquery-ui.custom.css" rel="stylesheet" type="text/css" />
  <link href="../../Content/themes/base/jquery.handsontable.full.css" rel="stylesheet" type="text/css" />
 <link href="../../Content/themes/base/demo-style.css" rel="stylesheet" type="text/css" />

 <script type="text/javascript">
    $(function () {

   var $container = $("#dataGrid");
 var $console = $("#dataConsole");
 var $parent = $container.parent();
 var autosaveNotification;
 $container.handsontable({
   startRows: 8,
   startCols: 6,
   rowHeaders: true,
   colHeaders: true,
   minSpareRows: 1



  var handsontable = $container.data('handsontable');

     $parent.find('button[name=save]').click(function () {
       $.ajax({
      url: "Controller/Action",
      data: {"data": handsontable.getData()}, //returns all cells' data
      dataType: 'json',
      type: 'POST',
      success: function (res) {
       if (res.result === 'ok') {
      $console.text('Data saved');
      }
      else {
      $console.text('Save error');
     }
    },
      error: function () {
    $console.text('Success!');
   }
  });
 });

 </script>


   <div id="dataGrid"></div>
    <div id="dataCoonsole"></div>

   <input type="button" value="Save" name="save">

【问题讨论】:

  • 您的 url 看起来不正确 Controller/Action,您使用的是 WebApi 吗?你想使用 fiddler 来捕获到 API 的网络流量跟踪,我不会担心缩小,这与 ajax 调用无关

标签: jquery asp.net


【解决方案1】:

你拼错了dataCoonsole

<div id="dataConsole"></div>

此外,您在控制台中看到的错误与您的保存错误无关。

【讨论】:

    【解决方案2】:

    我将按钮点击行改为 $("button").click(function () {} 并将html元素改为

      <button> save </button>.
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 2016-12-16
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多