admin管理员组

文章数量:1289548

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



/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

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

Share Improve this question edited Mar 6, 2013 at 14:34 Farhad-Taran asked Mar 6, 2013 at 14:28 Farhad-TaranFarhad-Taran 6,54017 gold badges69 silver badges129 bronze badges 4
  • are you using - JSON.stringify(data) - before adding it to the querystring? – David Commented Mar 6, 2013 at 14:29
  • have you tried using HttpUtility.UrlDecode() – Dave Hogan Commented Mar 6, 2013 at 14:32
  • The important line is surely before 579. Where does the json variable get assigned? – Joe Commented Mar 6, 2013 at 14:32
  • It looks like you the querystring parameter name is wrong. From that screenshot it looks like its is '0' – David Commented Mar 6, 2013 at 14:33
Add a ment  | 

4 Answers 4

Reset to default 5

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.

Why you are not using this

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

instead of querystring.

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

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

本文标签: chow to pass json in a querystringStack Overflow