views:

1595

answers:

0

Hi!

I am receiving an empty response when calling a web method using asp.net in Chrome but not IE nor FF. I get this behavior using the ASP PageMethod.func as well as using jquery ajax call.

I can 'fix' the symptom by adding a delay sleep call on the server which makes me believe code is just plain wrong somewhere. I have the webkit fix so that Chrome is using webkit (see first line of log).

I have created a simple test case. The log of pressing each button from left to right is shown. Basically with out a sleep call on the server the load fails > 95% of the time. With a sleep(0) call its 50% and with a large sleep 1 sec it rarely (if ever?) fails.

If I remove the sleep and turn on Fiddler it never fails. Something like the response not getting flushed??

Is there something obviously wrong with the test case or is this a bug.

With fiddler on the headers response looks like this and there is 51 bytes of data (not shown): 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

Thanks!!

Chris

Log:

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

Test Page

<%@ 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"&gt;
<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>

Server Code

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