jQuery 1.5.1 ajax JSON data is null on controller
My ajax call looking like that:
$.ajax({
type: "POST",
url: "/Home/GenerateExportReport",
data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}",
async: false,
cache: false,
dataType: "json",
//cont开发者_运维技巧entType: 'application/json; charset=utf-8',
success: function (data) {
alert("success");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + " text:" + textStatus + "request " + XMLHttpRequest);
}
});
My Controller:
[HttpPost]
public ActionResult GenerateExportReport(string data)
{
. . .
}
The call is working but JSON data didn't come tho controller fore some reason, string data
is NULL at every call.
I did try some fixes from this links
fix jQuery.ajaxSetup()
from that link JQuery 1.5 and new "Text JSON" datatype didn't help
fix contentType: "application/json; charset=utf-8"
, from that link Sending String Data to MVC Controller using jQuery $.ajax() and $.post()
didn't work either, when I am setting contentType: "application/json; charset=utf-8"
, I am getting error code 500 internal server error
Also as mentioned in one of the posts jQuery 1.5.1 breaks all ajax() calls that it could be jQuery validation plugin
so currently i did remove the reference to this script from my page at all.
Any thoughts?
P.S
string in data ({ 'fromDate': '2004-12-01', 'toDate' ... }
) is only an example, I have very big JSON string which I need to pass to controller:
["ONE", "0", "1", "0", "0", "0", "0", "0", "0", "1", "TWO", "281", "5174", "70", "3406", "1405", "300", "4632", "1522", "16790", "TREE", "13", "174", "4", "119", "32", "18", "94", "45", "499", "FOUR", "28", "931", "17", "755", "414", "17", "1138", "353", "3653", "FIVE", "2", "30", "0", "12", "8", "0", "12", "3", "67", "SIX", "13", "250", "7", "173", "77", "18", "247", "49", "834", "9am", "0", "2", "0", "0", "0", "0", "1", "1", "4", "SEVEN", "185", "2838", "45", "2100", "828", "314", "2324", "1223", "9857", "EIGHT", "173", "3662", "23", "1798", "612", "95", "2007", "445", "8815", "NINE", "308", "5277", "52", "3800", "1842", "154", "5548", "1910", "18891", "TEN", "17", "233", "3", "145", "69", "21", "199", "70", "757", "Total", "1020", "18572", "221", "12308", "5287", "937", "16202", "5621", "60168"]
You're not being a value to a field called "data" in your output, you're just setting a string to the output. To get this to appear, change your data value to this:
data: { data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}" },
This is awful though. I think you should do this instead:
data: { fromDate: '2004-12-01', toDate: '2011-12-01', requestorId: 1}
then
[HttpPost]
public ActionResult GenerateExportReport(DateTime fromDate, DateTime toDate, int requestorId)
{
...
}
ASP.NET MVC does not by default treat action arguments as JSON. If using ASP.NET MVC 3 you can try the JsonValueProviderFactory:
Put this code in Global.asax to register it:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
Alternatively use regular arguments:
$.ajax({
type: "POST",
url: "/Reports/GenerateExportReport",
data: {fromDate: '2004-12-01', toDate: '2011-12-01', requestorId: 1}
/* rest */
});
[HttpPost]
public ActionResult GenerateExportReport(DateTime fromDate, DateTime toDate, int requestorId)
{
/* implementation */
}
Please check whether the following code works for you:
var param = {};
param.fromDate = '2004-12-01';
param.toDate = '2011-12-01';
param.requestorId = '1';
$.ajax({
type: "POST",
url: "/Home/GenerateExportReport",
data: param,
async: false,
cache: false,
dataType: "json",
//contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("success");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + " text:" + textStatus + "request " + XMLHttpRequest);
}
});
Code in Controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GenerateExportReport()
{
//Code to get the data
Request.Form["fromDate"]
Request.Form["toDate"]
Request.Form["requestorId"]
}
精彩评论