2014-10-07 160 views
-1

我试过了一些开箱即用的方法,试图让我的价值进入我的ajax,但每次我得到js错误。希望你们中的一个人能够提供帮助。AJAX:将值传递给ajax方法

我有以下JQuery的阿贾克斯:

$("#usertasklist").click(function() { 
    $('#basicModal4').modal('hide'); 
    $.ajax({ 
     type: "GET", 
     url: "/Home/GetAjaxUserTasksExecuted", 
     data: { filterdate: variable1 }, 
     datatype: "html", 
     success: function (data) { 
      console.log("Succes!"); 
      var $titleDiv = $('#Modal5Label'); 
      $titleDiv.replaceWith("<h3><b>User Tasks Executed</b></h3>"); 
      var $detailDiv = $('#superdatadivthing3'); 
      $detailDiv.replaceWith(data); 
     }, 
     error: function() { 
      console.log("Error!"); 
     }, 
     complete: function() { 

      $('#basicModal5').modal('show'); 
      console.log("Complete!"); 
     } 
    }); 
}); 

在我的控制器中的viewbag

Session["Usercode"]; 

如何传递从我的观点袋值在数据段替换“变量1”我的ajax电话?

+0

忽略建议你动态地注入到你的脚本中的任何答案。这有各种各样的问题。取而代之的是将其注入到页面中,并用'$('body')。'data'('filter');'如果你必须注入脚本,使用简单的独立全局变量 – 2014-10-07 10:48:44

+0

为什么你需要从ajax获得已经在会话中的东西?如果我用ajax请求中的另一个用户代码更改变量1值,该怎么办? – 2014-10-07 10:49:52

+0

您可以使用隐藏控件并从那里获取值 – Tushar 2014-10-07 10:53:35

回答

0

如何从容器中获取数据?

HTML,例如

<table id="YOURDIVHERE"> 
    <tr> 
     <td> 
     <span>data which i will use</span> 
     </td> 
    </tr> 
</table> 

你的Ajax调用

$("#usertasklist").click(function() { 
    $('#basicModal4').modal('hide'); 
    var data = innerXHTML($('#YOURDIVHERE').attr('id')); 
     $.ajax({ 
     type: "GET", 
     url: "/Home/GetAjaxUserTasksExecuted", 
     data: { filterdate: data }, 
     datatype: "html", 
     success: function (data) { 
      console.log("Succes!"); 
      var $titleDiv = $('#Modal5Label'); 
      $titleDiv.replaceWith("<h3><b>User Tasks Executed</b></h3>"); 
      var $detailDiv = $('#superdatadivthing3'); 
      $detailDiv.replaceWith(data); 
     }, 
     error: function() { 
      console.log("Error!"); 
     }, 
     complete: function() { 

      $('#basicModal5').modal('show'); 
      console.log("Complete!"); 
     } 
    }); 
}); 
+0

你能发表一个div的例子吗?编辑:我按照你的建议创建了代码,但它不能识别innerXHTML。 – Baggerz 2014-10-07 11:01:23

+0

它基本上可以是div内的任何HTML。在“我的”div中,有一张表格,我在发送数据时使用电子邮件发送数据。我justed分析了tabledata服务器端来获取邮件内容。 – 2014-10-07 11:02:16