0

当前脚本:

var IrregularChartParams = InitializeChartParams();

// parametreleri json stringe cevir...
var chartParams = JSON.stringify(IrregularChartParams);

$.ajax({
    url: '/Widget/GridExportToExcel',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: chartParams,
    ....

控制器

public void GridExportToExcel(IrregularChartParams chartParams)
{

我想要以下内容:

// I know this does not work...
var url = '/Widget/GridExportToExcel' + chartParams
window.open(url);

使用模型绑定将 json 数据添加到 url 的简单方法是什么?

4

2 回答 2

1

可能是我无法解释,但我找到了解决方案,我不知道,它是最好的,但它有效

var IrregularChartParams = InitializeChartParams();
var url = '/Widget/GridExportToExcel?' + $.param(IrregularChartParams, true);
window.open(url);
于 2013-05-16T10:09:07.080 回答
1

1: 如果InitializeChartParams()函数,返回一个名称-值对,你不需要任何东西,但将请求方法更改为GET

var IrregularChartParams = InitializeChartParams();

// parametreleri json stringe cevir...
// var chartParams = JSON.stringify(IrregularChartParams);
var chartParams = IrregularChartParams; 

$.ajax({
    url: '/Widget/GridExportToExcel',
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    data: chartParams,
    ....

2: 如果InitializeChartParams()不返回名称-值集合,您可以将其内容作为查询字符串附加到 URL, by $.param,正如您在回答中提到的那样。

3: 如果需要,可以将IrregularChartParams作为单个查询字符串项传递给服务器,并在服务器上反序列化它:

var IrregularChartParams = InitializeChartParams();

var chartParams = JSON.stringify(IrregularChartParams);

$.ajax({
    url: '/Widget/GridExportToExcel',
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    data: {sp: chartParams},

and action method:

public void GridExportToExcel(string cp) {
    var chartParams = JsonConvert.DeserializeObject<IrregularChartParams>(json);
    ...
}
于 2013-05-16T10:17:55.143 回答