0

I have to send data to controller i tried in string fromat but after certain limit it is not accepting data...so i thought to convert it to json and send...or if there is any other way to send more data by Ajax please suggest me,plese tell me how can i convert my data to json format and how to convert it into string in controller,....

var param = "&tbl=" + tbldata;
        param = param + "&tblheader=" + tblheader;

        var request = $.ajax({
            url: '../rep/send?'+param,
            type: 'POST',
            cache: false,
            dataType: 'text',
            contentType: 'application/text; charset=utf-8'
        });

[HttpPost]
[ValidateInput(false)]
public ActionResult SendReport(string tbl, string tblheader)
{

}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
PANKAJ 786
  • 129
  • 1
  • 11

5 Answers5

0

If that tbldata is string then you can use the function called JSON.stringify(string) and in that Ajax dataType should be json.

For your reference :Difference between JSON.stringify and JSON.parse

Community
  • 1
  • 1
Tech Mahesh
  • 392
  • 1
  • 3
  • 14
  • this is not my problem i already tried this when i am sending data in string form it is accepting up to certain limit like param.lenth - 1576 likewise after that it is crashing my code so i thought convert it into json and send it to controller...if you have any solution so i can send more data in string form also pelase let me know – PANKAJ 786 Nov 18 '13 at 06:28
  • Then i hope you should modify web.config property – Tech Mahesh Nov 18 '13 at 07:00
  • i tried now this also not working man plese suggest something else – PANKAJ 786 Nov 18 '13 at 07:14
0

Try adding the following in your web.config file.

 <configuration>
   <system.web>
     <httpRuntime maxRequestLength="xxxx" />
   </system.web>
 </configuration>

The default value for maxRequestLength is 4096 (4MB).

Try setting something that is larger that 4MB. That may work for you.

Hope it helps.

Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
0

passing data in url has a limit depending on navigator (Example)

its better to pass it in request body :

var request = $.ajax({
   url: '../rep/send',
   type: 'POST',
   cache: false,
   dataType: 'json',
   data: {
          tbl: tbldata,
          tblheader : tblheader 
   },
   contentType: 'application/text; charset=utf-8'
});

Edit

create Model :

[DataContract]
public class ReportModel
{
    [DataMember]
    public string tbl {get; set; }

    [DataMember]
    public string tblheader {get; set; }
}

modify controller :

[HttpPost]
[ValidateInput(false)]
public ActionResult SendReport([FromBody] ReportModel data)
{

}
Pascalz
  • 2,348
  • 1
  • 24
  • 25
0

If you will use json (that is a good choice) you need to understand javscript object structure, serialization in client side and deserialization in server side.

Community
  • 1
  • 1
Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
0

remember to encode the data.

in javascript:

  var sJsonData = JSON.stringify({your data});
  ......
  contentType:"application/x-www-form-urlencoded; charset=UTF-8",
  data: "sJsonObject=" + encodeURIComponent(sJsonData),

so your case would look like:

        url: '../rep/send',
        type: 'POST',
        data: "sJsonObject=" + encodeURIComponent(JSON.stringify(para)),
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        ...

server side:

[HttpPost]
public ActionResult ajaxProcess(string sJsonObject = "")
string sUnescapeJsonData = System.Uri.UnescapeDataString(sJsonObject);
Victor Xie
  • 148
  • 1
  • 9