2011-06-07 95 views
1

在過去的幾個小時試圖調試這件事之後,我的頭撞在牆上,終於決定尋求幫助。AJAX發佈請求不會發送JSON數據

我有這樣的數據,我想發送到ashx處理程序(這是一個很好的數據)。

var value = [{"start":["3,0"],"block":["0,0","1,2"],"end":["2,1"],"star":"gold","moves":3,"difficulty":"easy"},{"start":["1,0"],"block":["1,3","3,0","4,2"],"end":["0,1"],"star":"gold","moves":4,"difficulty":"easy"},{"start":["3,0"],"block":["0,0","0,2","2,0","3,2"],"end":["1,0"],"star":"silver","moves":4,"difficulty":"easy"},{"start":["3,0"],"block":["0,0","2,0","3,1"],"end":["1,3"],"star":"gold","moves":6,"difficulty":"easy"},{"start":["0,0","2,0"],"block":["2,3"],"end":["1,2"],"star":"gold","moves":4,"difficulty":"easy"},{"start":["2,1"],"block":["0,1","0,2","1,0","1,1","2,0","2,2","2,3","3,1","3,2"],"end":["1,3"],"star":"gold","moves":5,"difficulty":"easy"},{"start":["1,0"],"block":["0,0","3,0","2,3"],"end":["4,1"],"star":"gold","moves":5,"difficulty":"medium"},{"start":["0,0","0,4"],"block":["0,5","0,2","3,3"],"end":["1,1"],"star":"gold","moves":7,"difficulty":"medium"},{"start":["0,0","2,6"],"block":["0,5","3,3","2,1"],"end":["3,5"],"star":"gold","moves":8,"difficulty":"medium"},{"start":["4,1","4,3"],"block":["3,0","4,2"],"end":["0,1","1,4","3,2"],"star":"gold","moves":8,"difficulty":"medium"},{"start":["1,2","3,4","4,2"],"block":["0,2","3,0"],"end":["2,3"],"star":"gold","moves":9,"difficulty":"medium"},{"start":["3,1","3,6"],"block":["0,0","0,3","0,7","2,5"],"end":["2,3"],"star":"gold","moves":11,"difficulty":"hard"},{"start":["0,7","0,2"],"block":["2,0","3,2","0,6","1,6","1,7"],"end":["3,3"],"star":"gold","moves":12,"difficulty":"hard"},{"start":["0,0","0,3"],"block":["0,1","2,2","3,0","3,3"],"end":["4,2"],"star":"gold","moves":8,"difficulty":"hard"},{"start":["0,0","0,6"],"block":["0,1","1,0","1,1","2,5","3,7"],"end":["3,4"],"star":"gold","moves":13,"difficulty":"hard"},{"start":["0,0","0,2","0,4","2,0","2,4","3,2","4,0","4,4"],"block":["0,1","0,3","1,0","1,1","1,2","1,3","1,4","2,1","2,3","3,0","3,1","3,3","3,4","4,1","4,2","4,3"],"end":["2,2"]},{"start":["0,0","0,2","0,4","1,1","2,0","2,4","3,2","4,0","4,2","4,4"],"block":["0,1","0,3","1,0","1,2","1,3","1,4","2,1","2,3","3,0","3,1","3,3","3,4","4,1","4,3"],"end":["2,2"],"star":"silver","moves":42,"difficulty":"medium"},{"start":["0,0","3,3","4,0"],"block":["0,1","2,3","3,0","4,4"],"end":["0,3"],"star":"gold","moves":11,"difficulty":"hard"},{"start":["0,4","1,1","3,5","4,2"],"block":["0,0","3,1","4,1"],"end":["2,3"],"star":"gold","moves":14,"difficulty":"hard"},{"start":["0,0","3,2","3,6"],"block":["0,4","0,5","4,4"],"end":["1,1"],"star":"gold","moves":13,"difficulty":"hard"},{"start":["0,2"],"block":["0,7","4,0","4,6","5,0","6,0","6,5"],"end":["2,0"]}] 

,我使用這個函數來發送請求:

function storeValue(value) { 
    var val = encodeURIComponent(JSON.stringify(value)); 
    $.ajax({ 
     url: "DataHandler.ashx", 
     async: false, 
     data: { key: "someKey", value: val, action: "store" }, 
     datatype: "json", 
     success: function (data) { 
     } 
    }); 
}; 

在DataHandler.ashx,這是相關代碼:

public class DataHandler : IHttpHandler, IReadOnlySessionState 
    { 
     public void ProcessRequest(HttpContext context) 
     { 
      var query = context.Request.QueryString; 
      string action = query["action"]; 
      string key = query["key"]; 
      string val = query["value"]; 
     } 
    } 

經過調試,我發現DataHander甚至沒有被調用。如果我刪除value從查詢字符串,像這樣:

data: { key: key, action: "store" }, 

ProcessRequest方法將被稱爲我所期望的。

我在猜測value可能太長了。爲什麼它不被髮送,我該如何解決?

+1

您是否試圖通過Fiddler或Firebug監視您的請求?此外,這是一個GET可能會導致你運行到一個URI +查詢字符串的最大長度,我認爲這是2000-4000字符限制。我認爲IE8有2048個限制。如果您通過Fiddler運行您的請求,您是否看到它被髮送出去?如果是這樣,你會在響應中看到什麼錯誤(如果有的話)? – 2011-06-07 18:18:14

+0

爲什麼你沒有在'$ .ajax'中添加'type:「POST」'?其他的方式,我認爲它的GET請求,並且數據必須在這樣一個字符串中給出:'var = val&var2 = val2'等等...... – nagisa 2011-06-07 18:23:37

+0

@David它有一個'400 Bad Request'錯誤。 – 2011-06-07 18:23:52

回答

1

當我運行我的測試代碼,我看到下面的錯誤從jQuery AJAX調用回來:

異常詳細信息信息:System.Web.HttpException:查詢字符串的這個長度 請求 超出了配置的 maxQueryStringLength值。

所以你的查詢字符串太長了(至少對於我正在測試的IE9)。

根據建議,將其更改爲POST允許在ASHX文件中訪問ProcessRequest方法。

你想也想改變的ProcessRequest從請求主體,而不是查詢字符串檢索值....

public void ProcessRequest(HttpContext context) 
{ 
    var query = context.Request; 
    string action = query["action"]; 
    string key = query["key"]; 
    string val = query["value"]; 
} 

我希望這有助於!