Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ajax missing Brace?

Options
  • 02-11-2007 4:00pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    This is my first Ajax script and I'm being told that I'm missing a closing brace. Heres my script:
    var xmlHttp;
    
    function getDays(){
    	xmlHttp = GetXmlHttpObject();
    	if (xmlHTTP==null){
    		alert ("Your Browser does not support Ajax");
    		return;
    	}
    	
    	var url = "Apage.php";
    		url = url + "?ran" + Math.random(); 
    		xmlHttp.onreadystatechange = stateChanged;
    		xmlHttp.open("POST",url,true);
    		xmlHttp.send("number=" + getElementById('number').value);
    }
    
    function stateChanged(){
    	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
    		document.getElementById("days").innerHTML = xmlHttp.responseText;
    	}
    }
    
    function GetXmlHttpObject(){
    	var xmlHttp = null;
    	try{
    		xmlHttp = new XMLHttpRequest();
    	}
    catch (e)
    	{
    		try{
    			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    	catch (e)
    		{
    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	}
    	return xmlHttp;
    }
    

    Its an external script and this is the error message I'm getting:
    missing } in XML expression (line 5);
    xmlHttp = GetXmlHttpObject();
    ----------------------------^
    
    As far as I can tell all braces have been closed so what am I doing wrong?


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    Not sure where the brace problem is but I got your code working by fixing a few problems. First of all this if (xmlHTTP==null) is using the wrong case. Don't forget javascript is case sensitive so if (xmlHttp==null) should fix that. Secondly

    xmlHttp.send("number=" + getElementById('number').value);

    should read

    xmlHttp.send("number=" + document.getElementById('number').value);

    Finally you are doing a post so you need to set the request header of the xmlHttp object:-

    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send("number=" + document.getElementById('number').value);

    Making these changes worked for me.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Yeah, thanks for those. There were also different problems here and there but the one that was causing the missing bracket error was because I had script tags in my external script. :P

    Its always the little mistakes that cause the problems.

    Thanks again.

    EDIT - I got it fixed, its working perfectly now. Thanks.


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Forget about IE for doing any JS debugging. There are options out there for doing this but they're crap.

    Use Firefox and the Firebug extension to track down the location of the error. That will often point to the cause.

    Often just walking away for a couple of minutes and then coming back to it removes the scales from your eyes that obscure these things.


Advertisement