Solved

Ajax having issues with subdomain names in the url

Posted on 2006-07-21
29
611 Views
Last Modified: 2008-02-26
This baffles me, but you guys are probably all over it.  I have an ASP application that uses AJAX to get data from the database, after which I parse it in javascript and render the HTML on the screen.  Now, when I navigate to the page using this convention:

http://mydomainname.com/folder/

Life is good, and the application functions normally.  However, when I try to do this:

http://folder.mydomainname.com/

The AJAX doesn't work.  The subdomain is set up properly, for I've been using an older version of this application at that same address for a while now, so I don't think there's an issue with my host.

In my AJAX, I do this:

http_request.onreadystatechange = renderResults;

function renderResults()
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      buildForm(http_request.responseText);
    }
    else
    {
      document.getElementById('TempSpan').innerHTML = "There was a problem with the request.";
    }
  }
}

I keep getting the "There was a problem with the request" message, which tells me that I never get the http_request.status == 200.  But, like I said before, when I use the fully-qualified url, it works.

I've had colleagues see similar issues in totally separate projects, so I am not convinced it's as simple as a type-o in my code.  Any clue as to why subdomain urls seem to mess up AJAX?

jmanGJHS97
0
Comment
Question by:jmanGJHS97
  • 15
  • 11
  • 2
  • +1
29 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 17158215
Let your script show the status code at readyStae 4. I assume you get some 3xx code for redirection.

0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17158218
Cross-Scripting limitations. Some browsers will allow more than others.
You can probably tweak your IE or FF settings to allow it, but each users will have to do that.
0
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 17158357
What page are you making the AJAX request to?  AJAX requests can only be performed against the same domain as they are executed from (ie, www.somewhere.com can only AJAX request www.somwhere.com).  A domain includes its subdomains but NOT it's parent domains.
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17158689
Zvonko,

I get status 404 at ready state 4.  This is when I use the subdomain as an address.  I get status is 200 at ready state 4 when I hit the page using the fully qualified name.

netsmithcentral,

The page I am trying to request from is inside the subdomain webroot folder, so I don't see any issues there.  The application is fully contained within the webroot of the subdomain.

jmanGJHS97
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17158770
Keep in mind that mydomainname.com and folder.mydomainname.com are different and unrelated domains for the browser.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17159168
Do you get 404 when you put the same subdomain URL in browser address bar, or do you get the correct page?
My assumption with the 3xx code was because browser can recognize the redirection code and look for the redirection header. The redirection lookup us not done automatically for you by ajax.
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17160548
Zvonko,
I'm not sure I follow your question.  When I navigate to this:
http://subdomain.domain.com/
I get the 404.  But, when I go here:
http://domain.com/folder
I don't get the 404.  What other url would you like me to try?

DireOrbAnt,
I understand what you are saying, but does that imply for me that I can't use subdomains in conjunction with AJAX, or is there a way to resolve that issue?

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17160665
If the browser does get 404 for http://subdomain.domain.com/ why then you expect 200 for ajax?
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161010
What SHOULD I be looking for?  When I use http://www.domainname.com/folder/, I get a status of 200, which is what I thought I should be looking for.  Is that incorrect?  Should I be looking for 404 instead?  I'm fairly new to AJAX, so if I am looking for the wrong status, please let me know.

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161026
Do you get the page in browser when you enter this URL in browser address bar: http://subdomain.domain.com/
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161039
Yes, the page renders.  However, on the page are several different sections that go out and get their content via AJAX, and they do not return properly.  The page does render, but the AJAX doesn't come back with data.

If this is not the question you are asking, then I don't understand the question.  "Do you get the page in browser" to me means "Does the page render", and the answer is yes, but the AJAX doesn't work.  The ASP works, but the AJAX does not, unless I use the fully qualified URL.

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161084
OK, now finaly I understood what you are asking, sorry for confusion.
So you say, when you page using ajax services is addressed as ttp://domain.com/folder then your ajax parts on those pages in that directory are working fine.
But when your page or pages containing ajax is addressed as http://subdomain.domain.com/ then are the ajax jobs failing.
In both scenarios do the html pages render fine (except the ajax parts) and so far you did NOT state any ajax URL, right?

So my recommendation is to place this statement on pages that are accessed trough this url: http://yoursubdomain.yourdomain.com/
<script>
document.doamin="yourdomain.com"; // reduces the domain for yoursubdomain.yourdomain.com to:  yourdomain.com
</script>

0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161094
Yes, you understand the situation.  Maybe I wasn't being clear, but I am glad you see the situation now.

What do you mean by "and so far you did NOT state any ajax URL, right?"?

I will insert that script and see what happens.  I assume that you meant:

document.domain
instead of
document.doamin

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161106
Uhps! you are right :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161145
That script didn't seem to help, but I did find something interesting in the code.  There are 6 AJAX sections on the page, and three of them work.  The other three don't.  Here is one difference I found between the set that work and the set that don't.

When I do this:
http_request.open('GET', url, true);
I have built the url prior.  In the three that work, I have something like this:
url = 'GetNullCategoriesForm.asp';
and in the three that don't, I have something like this:
url = 'GetBalanceForm.asp' + getQS('MyForm');

Here is the getQS function.  The purpose of this is to get form elements and build them into a string that I can pass as a querystring to my ASP page, so I can use form element values as inputs to my ASP code.  Does anything in this function help you see why the subdomain is causing issues?

function getQS(sFormName){
  var QS = "";
  var currentValue = "";
  var currentType = "";
  var currentName = "";
  if (sFormName.length>0){
    for (var i=0; i<document.forms[sFormName].elements.length;i++){
      currentValue = document.forms[sFormName].elements[i].value;
      currentType = document.forms[sFormName].elements[i].type;
      currentName = document.forms[sFormName].elements[i].name;
      if (currentType!="button" & currentType!="submit"){
        if (currentType=="select-multiple"){
          for (var o=0; o<document.forms[sFormName].elements[i].options.length;o++){
            if (document.forms[sFormName].elements[i].options[o].selected==true){
              QS+="&" + currentName + "=" + escape(document.forms[sFormName].elements[i].options[o].value);
            }
          }
        }
        else if (currentType=="checkbox"){
          if (document.forms[sFormName].elements[i].checked == true){
            QS+="&" + currentName + "=" + escape(currentValue);
          }
        }
        else{
          QS+="&" + currentName + "=" + escape(currentValue);
        }
      }
    }
    QS = "?" + QS.substr(1, QS.length) + "&rnd=" + Math.random();
  }
  else{
    QS = "?rnd=" + Math.random();
  }
  return QS;
}

jmanGJHS97
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161180
I just tested this theory by taking the getQS function call out of the url in one of the sections that didn't work initially, and that did make the AJAX work.  So, there has to be something in the function that is causing issues.

However, I cannot simply remove getQS because I need to be able to send the form element values to the ASP page because they determine what exactly I query and return.  So, we either need to fix this function or come up with an alternative way to get the form element values out of the page and sent to the ASP page via AJAX.

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161216
check this:

url = 'GetBalanceForm.asp?myform=' + getQS('MyForm');


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161221
you get 404 because you ask for such URLs: GetBalanceForm.aspsomepage.html

0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161242
When I build the url, I get this:

GetTransactionRegistry.asp?ClearPendingItems=0&ClearTransactions=&tb_Date=&tb_Payee=&tb_Amount=&sb_StatusID=0&sb_CategoryID=25&sb_SmartTagID=%25&tb_Notes=&SortColumn=&SortDirection=&PageNumber=&PageCount=&EditTransaction=&UpdateTransaction=&DeleteTransaction=&rnd=0.6306541227915277&AccountID=35

Everything in the querystring is the result of getQS.  The url is valid, is it not?

jmanGJHS97
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161245
The "?" is put in at the end of the function by either this:
QS = "?" + QS.substr(1, QS.length) + "&rnd=" + Math.random();
or this:
QS = "?rnd=" + Math.random();

jmanGJHS97
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17161311

Instead putting the query_string in URL send the fields like this:

http_request.send(getFields('MyForm'));

Do escape() the field values.
Put & before all field names, no question mark needed and also no randomizer needed.
Better put the cache supression to url like this:

url = 'GetBalanceForm.asp?time=' + (new Date()).getTime();

0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161445
ok, i will try that and get back to you soon.

thanks for your continued support.

jmanGJHS97
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161458
Just so I understand, what you are saying is do this:

url = 'GetTransactionRegistry.asp?time=' + (new Date()).getTime();
http_request.onreadystatechange = alertTransactionRegistry;
http_request.open('GET', url, true);
http_request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
http_request.send(getQS('MyForm'));

where:

function getQS(sFormName){
  var QS = "";
  var currentValue = "";
  var currentType = "";
  var currentName = "";
  if (sFormName.length>0){
    for (var i=0; i<document.forms[sFormName].elements.length;i++){
      currentValue = document.forms[sFormName].elements[i].value;
      currentType = document.forms[sFormName].elements[i].type;
      currentName = document.forms[sFormName].elements[i].name;
      if (currentType!="button" & currentType!="submit"){
        if (currentType=="select-multiple"){
          for (var o=0; o<document.forms[sFormName].elements[i].options.length;o++){
            if (document.forms[sFormName].elements[i].options[o].selected==true){
              QS+="&" + currentName + "=" + escape(document.forms[sFormName].elements[i].options[o].value);
            }
          }
        }
        else if (currentType=="checkbox"){
          if (document.forms[sFormName].elements[i].checked == true){
            QS+="&" + currentName + "=" + escape(currentValue);
          }
        }
        else{
          QS+="&" + currentName + "=" + escape(currentValue);
        }
      }
    }
  }
  return QS;
}

Is this what you are saying?

jmanGJHS97
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17161461
When I do http_request.send(getQS()), how do I retrieve the element values on the ASP page?  Can I still request.querystring and get them?

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17161500
No, then are the values no more query string parameters but form field values.
Like:
 fieldValue =  Request.Form("yourFieldName")

0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17162004
I am not getting the values on the ASP page when I request.form.  Here's my AJAX:

url = 'GetTransactionRegistry.asp?time=' + (new Date()).getTime();
http_request.onreadystatechange = alertTransactionRegistry;
http_request.open('GET', url, true);
http_request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
http_request.send(getQS('Checkbook'));

Do you see any errors?

jmanGJHS97
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17187840
This answer did solve the issue of the AJAX rendering, but I still can't request the values on the ASP page.  I will submit a new question to solve that problem.

Thanks for your help.

jmanGJHS97
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17187977
Uhps, sorry. I did miss this one.
0
 
LVL 9

Author Comment

by:jmanGJHS97
ID: 17188367
It's cool, feel free to answer the new one for more points...  :)

jmanGJHS97
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now