troubleshooting Question

How can I complete an ajax post as page is rendering for a first view?

Avatar of hitman3030
hitman3030 asked on
JavaScript.NET ProgrammingAJAX
4 Comments1 Solution339 ViewsLast Modified:
Here is what I am doing. I need some guidance in the best way to incorporate ajax to accomplish it.

I have developed a responsive site that works well with any device, including mobile-- but I want to deliver smaller images to speed up the loading for slow/smaller screens. Thus far this works:

1) I clock the download of a small (20k) image to get a rough estimate of client's connection.

2) Using jquery, I also get the screen width.

3) I then categorize the enviorment as width+speed (i.e. smallslow, smallfast, bigslow, bigfast)

4) I store this single variable in a cookie which is available on all subsequent page calls.

5) Image tags are loaded initially without a specified src in the aspx file.
In .net code on subsequent pages, I can retrieve the cookie and set images src using this variable in a select case.

THE PROBLEM:
The very first time a client request a page from the site, this cookie is not available to the .net Page Load event. I need to pass it back to the aspx page while the page is rendering that first time view.  I am new to ajax.

Debug in js shows value is present in the ajax function and I get no error.

But debug in .net does not stop on any break points, not page_load() or the special GetSpeed() WebMethod I created (See Version 2 below). It seems my ajax call is not firing the page on the server as I expected, and I have not been able to debug it.

VERISON 1 (FAILED):
Here is the ajax call after setting cookie. Notice I am just calling the exact same url the client hit, without passing a special function with it.

Even though I coded this to pass the value of the cookie, I have been told that really, all I need is the ajax call--because cookies get passed back anyway in the http header.

<head>
 
function setcookie{...}

   var sizespeed = getCookie('cSizeSpeed');
   $.ajax({ url: "http//www.upmyscore.com/lp", data: { "sizespeed": sizespeed }, type: "POST" });
</head>

Here is the read in the .net page_load event:
strSizeSpeed = System.Web.HttpContext.Current.Request.Form("sizespeed")
if len(strSizeSpeed ) = 0 then
        If Not IsNothing(System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed")) Then
            strSpeedSize = System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed").Value
        End If 
end if

VERSION 2 (FAILED):
 I created a webmethod in the page and explicitly call it in the ajax post:

 var sizespeed = getCookie('cSizeSpeed');
   $.ajax({ url: "http//www.upmyscore.com/lp/lp.aspx/GetSpeed", data: { "sizespeed": sizespeed }, type: "POST" });


In the .net page:

    <System.Web.Services.WebMethod> _
    Public Shared Function GetSpeed()
        Dim strSpeedSize As String = ""
        If Not IsNothing(System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed")) Then
            strSpeedSize = System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed").Value
        End If 
ASKER CERTIFIED SOLUTION
Slick812

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros