我正在使用以下代碼從ASP.net MVC應用程序請求數據。我也使用TcpTrace,以便我可以看到請求/響應。Firefox JQuery JSON問題
if (isInteger($('#txtDay').val()) && isInteger($('#txtMonth').val()) && isInteger($('#txtYear').val())) {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: strApi + 'wip/job/getsummary/' + $('#txtYear').val() + '/' + $('#txtMonth').val() + '/' + $('#txtDay').val(),
data: '{}',
dataType: 'json',
cache: false,
beforeSend: function(XMLHttpRequest) { ShowLoading(); },
success: function(data, textStatus) {
ShowJobSummaryList(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
HideLoading();
ShowStatus('unable to retrieve job summary list');
alert(XMLHttpRequest.statusText);
alert(textStatus);
},
complete: function(XMLHttpRequest, textStatus) {
HideLoading();
}
});
}
使用IE一切正常 - 內容類型正確設置爲application/json。然而Firefox下3.5.5,內容類型丟失:
OPTIONS /api/wip/job/getsummary/2009/11/25 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
這將導致ASP.net MVC返回XML。任何人都可以解釋爲什麼Firefox不發送內容類型?
您的操作方法返回什麼類型的操作結果? – 2009-11-26 21:30:06
action方法將返回包含序列化爲json的ActionResult對象(如果請求的內容類型爲application/json; charset = utf-8或xml作爲默認值 – markpirvine 2009-11-27 09:15:37
因此,您正在使用內置的JsonResult類型,例如:返回Json(objectToSerialize); – 2009-11-27 10:17:42