使用ajax调用中的datatable url属性将参数传递给服务器

时间:2016-12-29 08:59:46

标签: jquery ajax asp.net-mvc datatables

我使用数据表并在ajax请求中进行初始化我使用data- attribute设置url

...
 "ajax": {
            "url": $('#mypage').data('source'),
            "type": "GET",
}...

页面中嵌入的url看起来像这样

 <divid="mypage" data-source=@Url.Action("CalculateSomething")>
 ...
</dv>

在服务器端,我期待参数someId

public JsonResult CalculateSomething(int someId)
{
}

我的问题是:

  

如何使用datatable url属性将参数传递给服务器   在ajax电话上面?

更新 参数将作为javascript中的下拉元素的值,如

var someId = $('#mydropdown').val();

2 个答案:

答案 0 :(得分:1)

检查$.ajax上的记录。对于参数,您可以使用data属性。像这样:

 url: function(e) { return $('#mypage').data('source'); },
 type: "GET",
 data:
     {
         someId: function(e) { return $('#mydropdown').val(); }, //here is your param
     },
 dataType: "json", //it's better to declare datatype, becouse you using JsonResult in your controller response.

答案 1 :(得分:1)

有两种方法,一种是你可以在网址中连接如下:

 "ajax": {
            "url": $('#mypage').data('source')+"someId="+id,
            "type": "GET",
}

另一种方法是使用ajax的数据属性:

"ajax": {
            "url": $('#mypage').data('source')+"someId=123",
            "type": "GET",
            "data": function (data) {

                            data.someId= 123;

                        }
}
相关问题