Farhad-Taran
Farhad-Taran

Reputation: 6512

how to pass json in a querystring?

I am trying to send a JSON to the server by encoding it into the URI using Jquery.param but I get the following error.

window.location.href = BriefExportPath+$.param(JSON.stringify({
                        title: $('.ui-dialog-title').text(),
                        items: ko.utils.arrayMap(Neptune.BriefCountrySection.SelectedCountries(), function (item) {
                            return item.ItemName
                        })
                    }))



[CustomAuthorize(Definitions.RoleSonarAdmin)]
        public FileContentResult ExportCsv(string json)
        {
            var x = new System.Web.Script.Serialization.JavaScriptSerializer();
            object obj = x.DeserializeObject(json);
            //return File(Helpers.BriefCsvBytes.GetCsvBytes(items), "text/csv", title); 
            return null;
        }



http://dev.neptune.local/Briefs/ExportCsv?0=%7B&1=%22&2=t&3=i&4=t&5=l&6=e&7=%22&8=%3A&9=%22&10=B&11=r&12=i&13=e&14=f&15=+&16=C&17=o&18=u&19=n&20=t&21=r&22=y&23=+&24=L&25=i&26=s&27=t&28=%22&29=%2C&30=%22&31=i&32=t&33=e&34=m&35=s&36=%22&37=%3A&38=%5B&39=%22&40=A&41=f&42=r&43=i&44=c&45=a&46=%22&47=%2C&48=%22&49=A&50=m&51=e&52=r&53=i&54=c&55=a&56=s&57=%22&58=%2C&59=%22&60=A&61=s&62=i&63=a&64=%22&65=%2C&66=%22&67=E&68=u&69=r&70=o&71=p&72=e&73=%22&74=%5D&75=%7D

enter image description here

Upvotes: 4

Views: 12375

Answers (4)

user1193035
user1193035

Reputation:

var jObj = (JObject)JsonConvert.DeserializeObject(json);

Upvotes: 0

Quentin
Quentin

Reputation: 943097

From the manual for jQuery.param():

Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.

You are passing it the return value of JSON.stringify which is a string.

You need to pass it an object instead.:

var json = JSON.stringify(etc etc);
var url = BriefExportPath + $.param( { "json": json } );
location = url;

Your server side code will then need to extract the data from the json query key.

Upvotes: 5

Timothy Groote
Timothy Groote

Reputation: 8643

Your server expects the input to end up in a predictable variable name somewhere, yet you seem to try and put it in a GET variable named 0, as we can see in ExportCsv?0=

What your code expects is ExportCsv?json=

Make sure that you are passing the value to the correct GET variable name

Upvotes: 0

Kabir
Kabir

Reputation: 2156

Why you are not using this

$.ajax({
    url: url,
    type: "POST",
    data: JSON.stringify(data),
    contentType: "application/json",
    complete: callback
});

instead of querystring.

Upvotes: 0

Related Questions