在Chrome中的ajax请求期间空响应,但不是IE或FF

时间:2009-07-11 03:45:18

标签: asp.net ajax google-chrome response

我在Chrome中使用asp.net调用web方法时收到空响应,但不是IE或FF。我使用ASP PageMethod.func以及使用jquery ajax调用获得此行为。

我可以通过在服务器上添加延迟睡眠调用来“修复”症状,这让我相信代码在某处是完全错误的。我有webkit修复程序,以便Chrome使用webkit(请参阅第一行日志)。

我创建了一个简单的测试用例。显示从左到右按下每个按钮的日志。基本上在服务器上进行睡眠呼叫时,加载失败> 95%的时间。睡眠(0)调用50%,大睡眠1秒,很少(如果有的话)失败。

如果我取消睡眠并开启Fiddler,它永远不会失败。像回应没有被冲洗的东西?

测试用例是否存在明显错误,或者这是一个错误。

使用头部上的fiddler响应看起来像这样,并且有51个字节的数据(未显示):    HTTP / 1.1 200好的    服务器:ASP.NET Development Server / 9.0.0.0    日期:星期六,2009年7月11日03:29:10 GMT    X-AspNet-版本:2.0.50727    缓存控制:私有    内容类型:text / html;字符集= utf-8的    内容长度:51    连接:关闭

谢谢!

克里斯

日志:

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

0 个答案:

没有答案