And*_*erd 7 ajax wcf json gzip webhttpbinding
我有一个包含过滤文本框和列表框的网页.对文本框的修改会触发AJAX请求,该请求返回一个值数组,用于填充列表框.
我有时会遇到这些调用失败的问题,这取决于返回的数据大小.小型返回数据将导致错误,返回大型数据并成功处理.
只有当我使用大于4.2的jQuery版本时才会出现此问题.如果我使用jQuery版本4.2,我没有问题.
jQuery.ajax(
{
cache: false,
url: "../Services/CmsWebService.svc/GetAvailableVideosForCompany",
type: "GET",
complete: function (jqXHR, textStatus) {
var responseText = jqXHR.responseText;
jQuery('#debugConsole').text(responseText);
availableVideosPopulationState.isRunning = false;
setTimeout(populateAvailableVideosListBox, 100);
},
data: { "companyIdString": queryParameters.companyIdField,
"textFilter": queryParameters.filterText
},
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
alert(errorString);
},
success: function (data, textStatus, jqXHR) {
populateVideoListFromAjaxResults(data);
}
}
);
Run Code Online (Sandbox Code Playgroud)
如果返回两个元素,则以下是调试控制台的内容:
{"d":[{"__type":"ListEntry:#WebsitePresentationLayer","Text":"SOJACKACT0310DSN1.mpg - [SOJACKACT0310DSN1]","Value":"5565_5565"},{"__type":"ListEntry:#WebsitePresentationLayer","Text":"SOJACKACT0310DSN1Q.mpg - [SOJACKACT0310DSN1Q]","Value":"5566_5566"}]}
Run Code Online (Sandbox Code Playgroud)
但是如果返回一个元素:
{"d":[{"__type":"
Run Code Online (Sandbox Code Playgroud)
所以,当然,我们得到一个"未终止的字符串常量"错误.
我用fiddler做了一些调查.
在所有响应(甚至是成功的响应)上,fiddler显示错误:
Fiddler在会话# n1中检测到协议违规.
内容长度不匹配:响应标头指示n2个字节,但服务器发送n3个字节.
如果响应标头指示大于实际大小的大小,则结果仍可由浏览器解释.
如果响应标头指示的大小小于实际大小,则浏览器无法解释结果.
显而易见的假设是响应处理代码读取Content-Length
标题并且不读取比长度中规定的数据更多的数据.
我调查的下一步是比较jQuery版本1.6.1(中断)和版本1.4.2(不会中断)的请求/响应头.
jQuery 1.6.1请求标头:
GET /Web/Services/CmsWebService.svc/GetAvailableVideosForCompany?companyIdString=2&textFilter=3DSBDL2&_=1315869366142 HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:52200/Web/Admin/PlayerGroupEditor.aspx?groupid=76
Accept-Language: en-au
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host: localhost:52200
Connection: Keep-Alive
Cookie: .ASPXAUTH=CE853BBD860F40F0026400610074006D006500640069006100310000002B5387799D71CC01002B5B5D62C771CC0100002F0000006B119589A7305098A560E57515498C56ECB332035F300427CDA2B28205D5E6B6
Run Code Online (Sandbox Code Playgroud)
jQuery 1.6.1响应头
Run Code Online (Sandbox Code Playgroud)HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Mon, 12 Sep 2011 23:02:36 GMT X-AspNet-Version: 4.0.30319 Content-Encoding: gzip Content-Length: 140 Cache-Control: private Content-Type: application/json; charset=utf-8 Connection: Close
这是我使用jQuery 1.4.1时的请求标头.请注意,Accept
标头与jQuery 1.6.1值不同.
GET /Web/Services/CmsWebService.svc/GetAvailableVideosForCompany?_=1315870305531&companyIdString=2&textFilter=3DSBDL2 HTTP/1.1
Referer: http://localhost:52200/Web/Admin/PlayerGroupEditor.aspx?groupid=76
Content-Type: application/x-www-form-urlencoded
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*
Accept-Language: en-au
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host: localhost:52200
Connection: Keep-Alive
Cookie: .ASPXAUTH=CE853BBD860F40F0026400610074006D006500640069006100310000002B5387799D71CC01002B5B5D62C771CC0100002F0000006B119589A7305098A560E57515498C56ECB332035F300427CDA2B28205D5E6B6
Run Code Online (Sandbox Code Playgroud)
并回复jQuery 4.1.1:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 12 Sep 2011 23:31:46 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 131
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close
Run Code Online (Sandbox Code Playgroud)
所以明显不同的是,当通过jQuery 1.6.1进行调用时,使用gzip压缩响应,并且当通过jQuery 1.4.2进行调用时,响应不会被压缩.
"q=0.01"
字符串.(我能找到的最好的解释"q=0.01"
是在这里,但我不明白为什么我的服务实现将此解释为请求严重压缩响应.)
// Make the AJAX call, passing in the company id and the filter string
jQuery.ajax(
{
accepts: 'application/json, text/javascript, */*',
cache: false,
url: "../Services/CmsWebService.svc/GetAvailableVideosForCompany",
type: "GET",
complete: function (jqXHR, textStatus) {
var responseText = jqXHR.responseText;
jQuery('#debugConsole').text(responseText);
availableVideosPopulationState.isRunning = false;
setTimeout(populateAvailableVideosListBox, 100);
},
data: { "companyIdString": queryParameters.companyIdField,
"textFilter": queryParameters.filterText
},
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
alert(errorString);
},
success: function (data, textStatus, jqXHR) {
populateVideoListFromAjaxResults(data);
}
}
);
Run Code Online (Sandbox Code Playgroud)
因此,在所有这些调查之后,剩下的问题是,当响应是GZIP压缩时,为什么内容长度标题和实际内容长度之间存在差异?
我正在使用webHttpBinding的WCF服务.
小智 5
首先 - 非常好的问题.这个问题为我提供了足够的信息来解决我的问题.
我有一个类似的问题,并在此处发布修复程序 - 以便它可以帮助某人.
Ajax get&post请求在IE中返回null
在其余浏览器中工作正常,但在fiddler中看到'Response Header指示n个字节,但服务器发送nn个字节'消息用于请求.
显而易见的假设是响应处理代码读取Content-Length头并且不再读取任何数据
我也这么认为!
在这种情况下,我清楚了一件事.有些东西正在篡改请求/响应. 我尝试切换回旧版本的jQuery(如你的问题所述),但这没有帮助.
修复 - 我打开了我的应用程序的Web配置,并通读了它.模块中包含了一个来自telerik 的'RadCompression模块',一旦删除它,一切都开始正常工作.
众所周知,RadCompression模块存在错误,并且通过压缩响应会导致多个问题.
如果您遇到类似问题,请尝试检查可能拦截您的请求/响应的内容.
归档时间: |
|
查看次数: |
8808 次 |
最近记录: |