在 Chrome 中使用 asp.net 而不是 IE 或 FF 调用 Web 方法时,我收到一个空响应。我使用 ASP PageMethod.func 以及使用 jquery ajax 调用来获得这种行为。
我可以通过在服务器上添加延迟睡眠调用来“修复”症状,这让我相信代码在某个地方是完全错误的。我有webkit
修复,以便 Chrome 正在使用webkit
(请参阅日志的第一行)。
我创建了一个简单的测试用例。显示从左到右按下每个按钮的日志。基本上,如果服务器上没有睡眠调用,则负载失败 > 95% 的时间。使用 sleep(0) 调用它的 50% 并且使用大睡眠 1 秒它很少(如果有的话?)失败。
如果我删除睡眠并打开 Fiddler,它永远不会失败。诸如响应没有被冲洗之类的东西?
测试用例是否有明显错误,或者这是一个错误。
带有 fiddler 的标题响应看起来像这样,并且有 51 个字节的数据(未显示):
HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Sat, 11 Jul 2009 03:29:10 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 51
Connection: Close
日志:
0: Sys.Browser: [object Object], 530.5, WebKit
1: btnA -1
2: waiting for response now
3: btnJQA -1
4: waiting for response now
5: Result = EMPTY, textStatus =success
6: btnA 0
7: waiting for response now
8: Result = OnClick(.NET, page method, 0): 82 7/10/2009 1:01:01 PM
9: btnJQA 0
10: waiting for response now
11: Result = EMPTY, textStatus =success
12: btnA 1000
13: waiting for response now
14: Result = OnClick(.NET, page method, 1000): 84 7/10/2009 1:01:07 PM
15: btnJQA 1000
16: waiting for response now
17: Result = {"d":"OnClick(jquery, ajax, 1000): 85 7/10/2009 1:01:12 PM"}
测试页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest.aspx.cs" Inherits="PA.AjaxTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
var iCount = 0;
function log(s) {
var l = $('#taLog');
l.get()[0].value += iCount++ + ': ' + s + '\n';
l.attr({ scrollTop: l.attr("scrollHeight") });
}
function btnA_Click(iMilliSec) {
log("btnA " + iMilliSec);
PageMethods.OnClick(".NET", "page method", iMilliSec,
function(result, userContext, methodName) {
log('Result = ' + ((result != null) ? result : 'result was null'));
},
function(error, userContext, methodName) {
log('PM Response Error: ' + (error != null ? error.get_message() : "no_msg") + '\n\t' + userContext + '\n\t' + methodName);
});
log("waiting for response now");
}
function json_parse(data) {
return (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') ? JSON.parse(data) : eval('(' + data + ')');
}
function btnJQA_Click(iMilliSec) {
log("btnJQA " + iMilliSec);
$.ajax({
type: "POST",
url: "AjaxTest.aspx/OnClick",
data: "{'s1': 'jquery', 's2': 'ajax', 'iMilliSec': '" + iMilliSec + "'}",
timeout: 3000,
cache: false,
async: true,//false,
//Connection: 'close',
contentType: "application/json; charset=utf-8",
//dataType: "json", bypass eval of response for now
dataFilter: function(data) {
return data;
},
success: function(msg, textStatus) {
log('Result = ' + (msg == '' ? 'EMPTY, textStatus =' + textStatus : msg));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
log('Got an error = ' + (textStatus != null) ? textStatus : "no textstatus");
}
});
log("waiting for response now");
}
function btnLoopA_Click() {}
function btnLoopJQA_Click() { }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/js/webkit.js" />
</Scripts>
</asp:ScriptManager>
<input id="btnA" type="button" value="btnA" onclick='btnA_Click(-1)'/>
<input id="btnJQA" type="button" value="btnJQA" onclick='btnJQA_Click(-1)'/>
<input id="btnA_S0" type="button" value="btnA_0_sec" onclick='btnA_Click(0)'/>
<input id="btnJQA_S0" type="button" value="btnJQA_0_sec" onclick='btnJQA_Click(0)'/>
<input id="btnA_S1" type="button" value="btnA_1_sec" onclick='btnA_Click(1000)'/>
<input id="btnJQA_S1" type="button" value="btnJQA_1_sec" onclick='btnJQA_Click(1000)'/>
<input id="btnLoopA" type="button" value="btnLoopA" onclick='btnLoopA_Click()'/>
<input id="btnLoopJQA" type="button" value="btnLoopJQA" onclick='btnLoopJQA_Click()'/>
<textarea id="taLog" cols="120" rows="15" style='position: fixed; left: 10px; bottom: 10px; right: 10px'></textarea>
<script type="text/javascript">
log('Sys.Browser: ' + Sys.Browser.agent + ", " + Sys.Browser.version + ", " + Sys.Browser.name);
</script>
</div>
</form>
</body>
</html>
服务器代码
using System;
using System.Web;
using System.Web.UI;
using System.ServiceModel.Web;
using System.Web.Services;
namespace PA
{
public partial class AjaxTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {}
static int __iCounter = 0;
[WebMethod]
public static string OnClick(string s1, string s2, int iMilliSec)
{
if (iMilliSec >= 0)
System.Threading.Thread.Sleep(iMilliSec);
string sRet = "OnClick(" + s1 + ", " + s2 + ", " + iMilliSec + "): " + __iCounter + " " + DateTime.Now.ToString();
__iCounter++;
return sRet;
}
}
}