Ajax having issues with subdomain names in the url

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
LVL 9
jmanGJHS97Asked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:

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
 
ZvonkoSystems architectCommented:
Let your script show the status code at readyStae 4. I assume you get some 3xx code for redirection.

0
 
DireOrbAntCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
netsmithcentralCommented:
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
 
jmanGJHS97Author Commented:
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
 
DireOrbAntCommented:
Keep in mind that mydomainname.com and folder.mydomainname.com are different and unrelated domains for the browser.
0
 
ZvonkoSystems architectCommented:
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
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
If the browser does get 404 for http://subdomain.domain.com/ why then you expect 200 for ajax?
0
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
Do you get the page in browser when you enter this URL in browser address bar: http://subdomain.domain.com/
0
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
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
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
Uhps! you are right :)
0
 
jmanGJHS97Author Commented:
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
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
check this:

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


0
 
ZvonkoSystems architectCommented:
you get 404 because you ask for such URLs: GetBalanceForm.aspsomepage.html

0
 
jmanGJHS97Author Commented:
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
 
jmanGJHS97Author Commented:
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
 
jmanGJHS97Author Commented:
ok, i will try that and get back to you soon.

thanks for your continued support.

jmanGJHS97
0
 
jmanGJHS97Author Commented:
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
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
No, then are the values no more query string parameters but form field values.
Like:
 fieldValue =  Request.Form("yourFieldName")

0
 
jmanGJHS97Author Commented:
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
 
jmanGJHS97Author Commented:
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
 
ZvonkoSystems architectCommented:
Uhps, sorry. I did miss this one.
0
 
jmanGJHS97Author Commented:
It's cool, feel free to answer the new one for more points...  :)

jmanGJHS97
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.