Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts

    Ajax returns a weird value to my textarea

    Hey, I'm making an ajax script which should return an article value to a textarea but it returns in firefox nothing and shows in the error handler that it has found an unknown excpetion or in IE it shows this value in the textarea:
    Code:
    <html><body><h1> HTTP/1.1 500 Server Error</h1></body></html>
    Here's my script:
    AJAX:
    Code:
    function update(obj)
    {
    	var url="ajax.asp";
    	//url=url+"lang="+obj.options[obj.selectedIndex].value;
    	//url=url+"&sid="+Math.random();
    	xmlHttp.onreadystatechange=function(){
    		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    			document.getElementById("txt").value = xmlHttp.responseText;
    	} ;
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    
    function GetXml()
    {
    var xmlHttp=null;
    try
     {
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    ASP Code:
    Code:
    <%@Language="VBScript"%>
    <%Response.Buffer = True%>
    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    <%
    Dim Conn : Set Conn = Server.CreateObject("ADODB.Connection")
    Dim Rs : Set Rs = Server.CreateObject("ADODB.Recordset")
    Conn.Open "PROVIDER=Microsoft.Jet.Oledb.4.0; Data Source='" & Server.MapPath("../db.mdb") & "'"
    Rs.Open "SELECT * FROM contents WHERE page = 'index'",Conn' AND lang='" & Request.QueryString("lang") & "'",Conn
    Response.Write Rs("Content")
    %>
    </body>
    </html>
    Any help would be appreciated!

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    The page doesn't let me edit so I'll just add this:
    When I view the ASP page it shows the article perfectly so I'm pretty sure the problem is with the script.

  • #3
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    When I tried viewing the status of the xmlHttp on the onreadystatechange it showed me that it's at 500 and it works only in IE.

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    500 error means there was a problem on the page that was being requested. Try doing a window.open with the url your are peicing together there.

    You should also check that the repsonse status = 200 before doing anything with it. If it doesn't = 200 then notify the user (or yourself) that there was a problem with the request.
    Helping to build a bigger box. - Adam Matthews

  • #5
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Hey. Thanks for the reply.
    I've tried doing what you've said and the window.open worked.
    The status of the request is 500. What should I do?

    Thanks again!

  • #6
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    I'd suggest backing out your code on the page you are requesting. Maybe start with just some simple text and add code until you recieve the error. That should narrow it down.

    Off the bat I would suggest checking that your recordset returned results prior to accessing it's data. You also want to make sure you escape single quotes from user submitted data to be utilized in a SQL query, else you are vulnerable to SQL injection.
    I don't think that part in red should be there either.



    ie.
    Code:
    Rs.Open("SELECT * FROM contents WHERE page = 'index'",Conn' AND lang='" & Replace(Request.QueryString("lang"),"'","''") & "'",conn)
    
    If Not rs.EOF Then
    Response.Write Rs.Fields("Content")
    End If
    Last edited by Basscyst; 09-12-2007 at 10:26 PM.
    Helping to build a bigger box. - Adam Matthews

  • #7
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Hey. Thanks for the reply.
    The conn part defines the connection I use so it has to be there.
    I tried what you'd said before and it still doesn't work. (Still returns the status 500).
    I've changed the name of the page to asd.asp because I thought maybe ajax is a reserved word although it was in quotes. Then I've minimized the document to only "Hello".
    Here's my code now:

    Index:
    Code:
    function update(obj)
    {
    	var xmlHttp = GetXml();
    	var url="asd.asp";
    	//url=url+"lang="+obj.options[obj.selectedIndex].value;
    	//url=url+"&sid="+Math.random();
    	xmlHttp.onreadystatechange=function(){
    	//
    		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{
    			if(xmlHttp.status == 200)
    				document.getElementById("txt").value = xmlHttp.responseText;
    			else
    				alert("xmlHttp.status = " + xmlHttp.status);
    		}
    	} ;
    	//window.open(url);
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    asd.asp
    Code:
    Hello

  • #8
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Well then, here is some code that may help. This is what I use for my Ajax stuff.

    This handles the request object, the data you wish to submit to the server, and the call back function.

    Code:
    //*******************************************************
    // Sends an asyncronous xmlhttp request using post
    // Pass the URL, Querystring, and callback function
    //*******************************************************
    function getReqObjPost(url,params,func){
    	var xmlhttp=false;
    	if(window.XMLHttpRequest){
        	try{
    			xmlhttp = new XMLHttpRequest();
            }catch(e){
    			xmlhttp = false;
            }
        }else if(window.ActiveXObject){
           	try{
            	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
          	}catch(e){
            	try{
              		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            	}catch(e){
              		xmlhttp = false;
            	}
    		}
        }
        if(xmlhttp){
    	  	xmlhttp.onreadystatechange=function(){
    	  		if(xmlhttp.readyState==4){	
    				if(xmlhttp.status==200){
    					var str=xmlhttp.responseText;
    					var xml=xmlhttp.responseXML;
    					func(xml,str);
    				}else{
    					handleErrFullPage(xmlhttp.responseText)
    				}
    			}
    	  	}
    		var now=new Date();
    		params=params + "&cdate"+now.getSeconds()+"=" + encodeURIComponent(now);
    		xmlhttp.open("POST",url,true);
    		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    		xmlhttp.send(params);
    	}
    }
    This creates a post string out of all form elements that are children of the node passed to it.

    Code:
    //*******************************************************
    // Builds a querystring of of the child form elements of the 
    // object passed to the function. 
    //*******************************************************
    function formPost(top_element){
    	var inputs=top_element.getElementsByTagName('*');
    	var qstring=new Array();
    	for(var i=0;i<inputs.length;i++){
    		if(!inputs[i].disabled&&inputs[i].getAttribute('name')!=""&&inputs[i].getAttribute('name')){
    			qs_str=inputs[i].getAttribute('name')+"="+encodeURIComponent(inputs[i].value);
    			switch(inputs[i].tagName.toLowerCase()){
    				case "select":
    					if(inputs[i].getAttribute("multiple")){
    						var len2=inputs[i].length;
    						for(var j=0;j<len2;j++){
    							if(inputs[i].options[j].selected){
    								var targ=(inputs[i].options[j].value) ? inputs[i].options[j].value : inputs[i].options[j].text;
    								qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);	
    							}
    						}
    					}
    					else{
    						var targ=(inputs[i].options[inputs[i].selectedIndex].value) ? inputs[i].options[inputs[i].selectedIndex].value : inputs[i].options[inputs[i].selectedIndex].text
    						qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
    					}
    				break;
    				case "textarea":
    					qstring[qstring.length]=qs_str;
    				break;
    				case "input":
    					switch(inputs[i].getAttribute("type").toLowerCase()){	
    						case "radio":
    							if(inputs[i].checked){
    								qstring[qstring.length]=qs_str;	
    							}
    						break;
    						case "checkbox":
    						if(inputs[i].value!="on"&&inputs[i].checked){
    							qstring[qstring.length]=qs_str;	
    						}
    						var stat=(inputs[i].checked) ? "on" : "off"
    						qstring[qstring.length]=inputs[i].getAttribute('name')+"="+stat;	
    						break;	
    						case "text":
    							qstring[qstring.length]=qs_str;
    						break;
    						case "password":
    							qstring[qstring.length]=qs_str;
    						break;
    						case "hidden":
    							qstring[qstring.length]=qs_str;
    						break;
    					}
    				break;	
    			}
    		}
    	}
    	return qstring.join("&");
    }
    Here is an example of the code in action:

    http://www.rateprice.com/ajaxhelp.asp

    Notice when you specify the callback function, you pass xml and str, if you access the xml variable it will give you the responseXML, and the str will give you the response text.


    This code pops up your error server side error message if things don't go right:

    Code:
    function handleErrFullPage(strIn) {
    
            var errorWin;
    
            // Create new window and display error
            try {
                    errorWin = window.open('', 'errorWin');
                    errorWin.document.body.innerHTML = strIn;
            }
            // If pop-up gets blocked, inform user
            catch(e) {
                    alert('An error occurred, but the error message cannot be' +
                            ' displayed because of your browser\'s pop-up blocker.\n' +
                            'Please allow pop-ups from this Web site.');
            }
    }
    Hope that helps.
    Helping to build a bigger box. - Adam Matthews

  • #9
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Hey. I appreciate your help very much!
    I really want to make this thing simplier. I don't want to load too many functions.
    Is there anyway I could just fix my current code?

    Thanks again!

  • #10
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    I solved the problem! It was the <base href="..." /> that caused the problem.
    Now I have a different problem but I'll post it in a new thread.

    Thanks for all the help!

  • #11
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Well, in the long run the first function will actually save you from loading more functions. It allows you to simply call that function anytime you need to make an ajax call.

    The second function is optional, it's just an easy way to post your form data to the page you are requesting.

    The third function is a debugging tool, so you can see what's going on on the requesting page when you get an error.

    Your choice though. Good luck to you.
    Helping to build a bigger box. - Adam Matthews

  • Users who have thanked Basscyst for this post:

    BarrMan (09-15-2007)

  • #12
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Forgot to thanks you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •