Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax calls involving posting 1

Status
Not open for further replies.

MarkXaml

Programmer
Oct 19, 2007
58
US
I am attempting to adapt the code in the FAQ here:

I want to have a ASP.NET login with links back to classic ASP. I made a .NET page (localmachine/ClassicSessionSync.aspx) that sets up the session variables into form elments and post it to a page (dev_intranet/DotNetSessionSync.asp) on the classic site page I made to take those elements and make them classic session variables. If I navigate to ClassicSessionSync.aspx, it works as expected.

Now, I tried using Ajax to call ClassicSessionSync.aspx and I have all sorts of problems. First of all, it returns 404 for me no matter how I put the address in. Is this caused by the fact that I'm posting to not only another page, but another server?

here is the javascript:
Code:
var xhr = false;
var vdivID;

function displayResults(vDivID) {
    // set divID that will update
    vdivID = vDivID;
    var url = "[URL unfurl="true"]http://localVirtualDirectory/V2/Common/ClassicSessionSync.aspx";[/URL]
    makeRequest(url);
    return false;
}

function makeRequest(url) {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        xhr.onreadystatechange = showContents;
        xhr.open('GET', url + '&ms=' + new Date().getTime(), true);    //+'?ms='+ new Date().getTime() keeps from caching
        xhr.send(null);
    }
    else {
        document.getElementById(divID).innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
    }
}

function showContents() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = "Remote Login Complete";
        }

        else {
            var outMsg = "There was a problem with the request " + xhr.status;
        }
        $(vdivID).innerHTML = outMsg;
    }
}

I also tried running the page like this (called from a Page_Load event:

Code:
protected void SessionSync()
    {
        bool returnValue;
        string url = "[URL unfurl="true"]http://localVirtualDirectory/V2/Common/ClassicSessionSync.aspx";[/URL]
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "HEAD";
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                returnValue = (response.StatusCode == HttpStatusCode.OK);
            }
        }
        catch (WebException)
        {
            returnValue = false;
        }
    }

Am I trying to do something that just plain isn't allowed?

Please pardon my ignorance and I look forward to any assistance anyone can offer.
 
the first thing I would do is use a js library that handles the ajax for you. this will greatly simplify the code and allow you to focus on the business aspect of system, not the infrastructure. all the major js libraries have an ajax implementation. I use jquery.

making an ajax request shouldn't make a difference. my guess... and that's all this is... is the ajax plumbing is incorrect so the request is poorly formed and you get 404. using a js library could take care of most of this.

you also mentioned a remote server for the asp/asp.net applications. without a web farm configuration to sync IIS/sessions across all the servers there is no way for server 1 IIS to know anything about server 2 IIS.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks for the tip, Jason. I've used jquery before, but very little. I'm not implementing any other jquery functions in this app, so I hadn't included it for fear of bloating the page load.

Now, when I run the code behind SessionSync(), I do not get the 404, and the status returns OK, but the result is not what I expected. Using the HttpWebRequest, will it actually run the javascript at the bottom of the page to submit it to the other server? I'm assuming not based on my results.


Thank you so much for your input!

Mark
 
Using the HttpWebRequest, will it actually run the javascript at the bottom of the page to submit it to the other server?
I don't have the answer to that.

js is a function of the browser, not the server, so the js would need to execute prior to sending the request and/or after the request is received. in either case the client is processing the js, not the server.

the HttpWebRequest doesn't process the response, it only feeds the http pipeline. the processing is left to how the response is handled.

this is my understanding of how HttpWebRequest is used.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
that's what I was looking for. Time to try a different approach!

Thanks for your time, Jason!

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top