0

The issue is as follows:

Everytime I make an ajax call using the script below the error exception is triggered instead of success. As you can see, the XML output is in the correct mime type and character set. The URL is local so there's no cross domain issues. I cannot for the life of me figure out why its returning a null value.

I would really appreciate any help you guys could give. I'm willing to answer any questions.

here is the link to the XML output. Ask you can see, text/xml and UTF-8

here is the link to the test page

function kill_auto_bday(){
    document.getElementById('mydiv').value = '';
    document.getElementById('mydivid').value = '';
    document.getElementById('mydiv').className='show_div';      
    document.getElementById('mydivid').className='show_div';                                                                        
    document.getElementById('sub_button').className='hide_div';                                         
    document.getElementById('this_error_now').className='hide_div';                                         
    setdefaultselection();  
}

function setdefaultselection(){
    for (var i=0; i < document.questionnaire_form.gender.length; i++) 
    {
        if (document.questionnaire_form.gender[i].value == "") 
        {
            document.questionnaire_form.gender[i].selected = true;
        }
    }
}

function getbdaystuff() {
        document.getElementById('this_error_now').className='hide_div';     
        document.getElementById('mydiv').className='hide_div';      
        document.getElementById('mydivid').className='hide_div';                                                                    
        document.getElementById('sub_button').className='hide_div';                                                 
        get_my_bday();
    }

function get_my_bday() {     
    var e = document.getElementById("gender");
    var genderselect = e.options[e.selectedIndex].value;
    var this_surv_id = document.getElementById("this_surv_id").value;       
    var bday_day = document.getElementById("bday_day").value;       
    var bday_month = document.getElementById("bday_month").value;   
    var bday_year = document.getElementById("bday_year").value;             
    var fu_url = "/ajax_results.cfm?ajax_type=auto_bday&param="; // The server-side script
    var go_url = fu_url + escape(genderselect)+ '&this_surv_id='+ escape(this_surv_id) + '&bday_day=' + escape(bday_day) + '&bday_month='  + escape(bday_month) + '&bday_year='  + escape(bday_year);
    document.getElementById('this_error_now').className='hide_div';                                         

    var response_obj = $.ajax({
        async: false,
        type: 'GET',
        dataType: 'xml',        
        url:'/ajax_results.cfm?ajax_type=auto_bday&param=1&this_surv_id=656&bday_day=27&bday_month=4&bday_year=1970',
        success: function (html) {
        alert('successful : ' + html);
        },
    error: function (error) {
    alert('error; ' + eval(error));
        }); 
}

Here is the XML in case you're wondering what it looks like:

<?xml version="1.0" encoding="UTF-8"?>
<mycontent> 
    <mydiv>No matching division found.</mydiv> 
    <mydivid>0</mydivid> 
    </mycontent>
  • After re-reading seems like it's not a duplicate. There are so many of those questions everyday that it looked like another one. – elclanrs Jun 25 '13 at 07:02
  • @elclanrs Not a problem. Any ideas? – Jeffrey Shain Jun 25 '13 at 07:04
  • Show us the result of `console.log(go_url)` – MrCode Jun 25 '13 at 07:05
  • Now that you just edited the question it does look like the problem described in the duplicate after all... – elclanrs Jun 25 '13 at 07:05
  • @elclanrs I'm just desperately trying different things to no effect. Would appreciate it if you refrained from saying how it's a duplicate and offer some assistance. Been troubleshoot this for 10 hours. – Jeffrey Shain Jun 25 '13 at 07:09
  • 1
    AJAX is **Asynchronous** that's the key. When you do `alert(response_obj)` the AJAX call won't be finished and `response_obj` will be `undefined`. You have to do any logic with the result _inside_ the callbacks. Read the dup in depth, I'm pretty sure that's the problem here. – elclanrs Jun 25 '13 at 07:11
  • That is true, but this is the main issue I guess `Everytime I make an ajax call using the script below the error exception is triggered instead of success. ` – Balint Bako Jun 25 '13 at 07:11
  • @BalintBako: Yes, that's seems to be the other problem but maybe on the server code... – elclanrs Jun 25 '13 at 07:13
  • @elclanrs I read the call. So should I be putting a 5 second timeout as suggested here? I mean I appreciate the help, but telling me to "read the dup properly" is rather condescending. I am not as well versed in jquery and AJAX as some here and its not readily apparent to me. I'd just appreciate some assistance thats a little more substantial than "go read this". I mean, I appreciate it, and I will, but telling someone to read a book on heart surgery doesnt mean theyre ready to operate. – Jeffrey Shain Jun 25 '13 at 07:16
  • @elclanrs the server code returns the XML without issue. See link in the question or you can go here: http://www.playerspace.com/ajax_results.cfm/ajax_type/auto_bday/param/1/this_surv_id/656/bday_day/27/bday_month/4/bday_year/1970 – Jeffrey Shain Jun 25 '13 at 07:17
  • 2
    Didn't mean it like that I edited the wording shortly after I posted the comment. I don't know why the error callback runs. The point of that answer is that you can't return things from AJAX requests because you don't know at which point that's going to happen so any code that needs to run _after_ the request must run inside the callbacks, such as in your case `alert(response_obj)`. The error has to be something on the server.. What does `alert(XMLObj)` _inside_ the callback say? – elclanrs Jun 25 '13 at 07:20
  • @elclanrs No response. I modified the javascript in the original post so you can see what i mean. – Jeffrey Shain Jun 25 '13 at 07:35
  • @JeffreyShain Use `console.log()` instead of `alert()`, and use it on the error event, either that, or the complete event. – Daedalus Jun 25 '13 at 07:36
  • @JeffreyShain Right now your syntax is bugged; you forgot a bracket. – Daedalus Jun 25 '13 at 07:39
  • @Daedalus It says "statusText: "Error: NETWORK_ERR: XMLHttpRequest Exception 101"" – Jeffrey Shain Jun 25 '13 at 07:39
  • @JeffreyShain You'll want to fix your domains then, because as far as I can tell, like Balint has said, this is a cross domain issue. – Daedalus Jun 25 '13 at 07:42
  • @daedalus See below. I am not referencing the domain in my ajax call at all. Only the file name. "url:'/ajax_results.cfm?" – Jeffrey Shain Jun 25 '13 at 07:43
  • @JeffreyShain I see you updated the location of the file, and as far as I can tell, it's working. You should accept the answer below, since that was the problem. – Daedalus Jun 25 '13 at 07:50

1 Answers1

2

It is a cross domain call (http://www.something.com is not the same as http://something.com), which can cause this.

If you get a redirect form the the http://something.com to http://www.something.com that will make it a cross-domain call too.

Balint Bako
  • 2,500
  • 1
  • 14
  • 13
  • I'm calling a local file. I'm not specifying a domain.The actual URL passed is something like "/ajax_results.cfm/ajax_type/auto_bday/param/1/this_surv_id/656/bday_day/27/bday_month/4/bday_year/1970" – Jeffrey Shain Jun 25 '13 at 07:11
  • @JeffreyShain As in a file:// url? – Daedalus Jun 25 '13 at 07:24
  • Someting is wrong with the xml... `XML Parsing Error: no element found Location: moz-nullprincipal` is displayed in the error console of FF. Is there a BOM at the beginning or it might be some encoding issue... I know it is displayed in the browser, but it is not good enough for the JS engine. The internet says that this is usually a cross-domain call issue, hence my next comment. – Balint Bako Jun 25 '13 at 07:24
  • How are you testing it? What is the test URL in your browser and what is the exact xml path (if not the one above)? – Balint Bako Jun 25 '13 at 07:25
  • Sorry try this. I was using an SEO friendly url. : http://www.playerspace.com/ajax_results.cfm?ajax_type=auto_bday&param=1&this_surv_id=656&bday_day=27&bday_month=4&bday_year=1970 – Jeffrey Shain Jun 25 '13 at 07:26
  • Well this is the xml and where are you trying to load it with AJAX? localhost? (I'm quite certaion it is a cross-domain call) – Balint Bako Jun 25 '13 at 07:28
  • I modified the above script in my original post to reflect a hard coded URL. – Jeffrey Shain Jun 25 '13 at 07:28
  • 1
    Then what I just said in the answer... – Balint Bako Jun 25 '13 at 07:29
  • @BalintBako I dont understand what you mean. Is not a cross domain call www.domain.com to www.domain.com2 (or subdomain.domain.com)? – Jeffrey Shain Jun 25 '13 at 07:32
  • 2
    The `www.` at the start makes it another domain. – Balint Bako Jun 25 '13 at 07:34
  • @JeffreyShain It is as Balint has said.. `http://name.com` is different than `http://www.name.com` – Daedalus Jun 25 '13 at 07:34
  • @daedalus Yes but as you can see in my code I am not using a domain at all. I am simply referencing a file in the web root. See? "url:'/ajax_results.cfm?" There is no domain. – Jeffrey Shain Jun 25 '13 at 07:42
  • 1
    I see your point @JeffreyShain and you will love the answer :) You are getting redirected to `www.` domain automatically! (check the URL of the xml without `www.` in the browser) – Balint Bako Jun 25 '13 at 07:44
  • @Daedalus "Is there a reason you've had to repeat this so much?" Your tone is not appreciated sir. I am a fellow developer asking for assistance. If you want to be condescending, please take it elsewhere. It's very difficult to clarify such things in this limited conversational format, and I have used "/#script_name#" in AJAX urls before without issue. Have a little patience. Nobody is asking you to be here. – Jeffrey Shain Jun 25 '13 at 07:52
  • @JeffreyShain Deleted; forgive me, it is currently 1 am where I am.. well, 12:53 am as of this comment, but that's aside from the point. This answer solved your problem, no? – Daedalus Jun 25 '13 at 07:54
  • 1
    @BalintBako NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. I had a global redirect for all non www URL calls to redirect to www. MORON. MORON. MORON I am. Wasted an entire #%%(*& day on this. LOL. Well thank you anyway, guys. I'm sorry I'm a complete dummy. Hellooooo. McFlyyyy. – Jeffrey Shain Jun 25 '13 at 08:02