2011-03-16 60 views
0

我的Ajax調用看起來就像是:的jQuery 1.5.1 AJAX JSON數據是在控制器

$.ajax({ 
     type: "POST", 
     url: "/Home/GenerateExportReport", 
     data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}", 
     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); 
     } 
    }); 

我的控制器:

[HttpPost] 
public ActionResult GenerateExportReport(string data) 
{ 
    . . . 
} 

的呼叫工作,但JSON數據沒來壽控制器出於某種原因,string data在每次調用時均爲NULL。

我也嘗試了一些修正從這個鏈接

修復jQuery.ajaxSetup()從該鏈接JQuery 1.5 and new "Text JSON" datatype沒有幫助

修復的contentType:"application/json; charset=utf-8",從該鏈接Sending String Data to MVC Controller using jQuery $.ajax() and $.post() 也不能工作,當我設置contentType: "application/json; charset=utf-8",我得到錯誤代碼500內部服務器錯誤

也正如其中一個帖子jQuery 1.5.1 breaks all ajax() calls所述,它可能是jQuery validation plugin所以目前我沒有刪除r從我的網頁中引用此腳本。

有什麼想法?

PS 字符串中的數據({ 'fromDate': '2004-12-01', 'toDate' ... })僅僅是一個例子,我有非常大的JSON字符串,我需要傳遞給控制器​​:

["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"] 

回答

0

你不是一個值到一個名爲場「數據「,你只需要在輸出中設置一個字符串即可。要得到這個,請將您的數據值更改爲:

data: { data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}" }, 

雖然這很糟糕。我認爲你應該做這個:

data: { fromDate: '2004-12-01', toDate: '2011-12-01', requestorId: 1} 

然後

[HttpPost] 
public ActionResult GenerateExportReport(DateTime fromDate, DateTime toDate, int requestorId) 
{ 
    ... 
} 
0

ASP.NET MVC默認治療操作的參數作爲JSON沒有。如果使用ASP.NET MVC 3,你可以嘗試JsonValueProviderFactory:

將這個代碼在Global.asax中註冊它:

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); 

或者使用常規參數:

$.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 */ 
} 
0

請檢查是否以下代碼適用於您:

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

控制器中的代碼

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult GenerateExportReport() 
    { 
    //Code to get the data 
    Request.Form["fromDate"] 
    Request.Form["toDate"] 
    Request.Form["requestorId"] 
    }