AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

Pass json data to URL?

current script:

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,
    ....

Controller

public void GridExportToExcel(IrregularChartParams chartParams)
{

I want some thing like following:

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

What is the easy way to add json data to url with model binding?

Upvotes: 0

Views: 2598

Answers (2)

amiry jd
amiry jd

Reputation: 27585

1: If InitializeChartParams() function, returns a name-value pair, you do not need to any thing, but change the request method to 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: If InitializeChartParams() does not return a name-value collection, you can attach its content to the URL as query string, by $.param, as you mentioned in your answer.

3: and if you want, you can pass the IrregularChartParams as a single query string item to the server, and deserialize it at server:

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);
    ...
}

Upvotes: 1

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

May be I cant explain, but I found a solution, I dont know, it is the best, but it works

var IrregularChartParams = InitializeChartParams();
var url = '/Widget/GridExportToExcel?' + $.param(IrregularChartParams, true);
window.open(url);

Upvotes: 1

Related Questions