Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2007, 11:39 AM   PM User | #1
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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!
BarrMan is offline   Reply With Quote
Old 09-12-2007, 11:43 AM   PM User | #2
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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.
BarrMan is offline   Reply With Quote
Old 09-12-2007, 04:10 PM   PM User | #3
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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.
BarrMan is offline   Reply With Quote
Old 09-12-2007, 07:28 PM   PM User | #4
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
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
Basscyst is offline   Reply With Quote
Old 09-12-2007, 09:34 PM   PM User | #5
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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!
BarrMan is offline   Reply With Quote
Old 09-12-2007, 10:16 PM   PM User | #6
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
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
__________________
Helping to build a bigger box. - Adam Matthews

Last edited by Basscyst; 09-12-2007 at 10:26 PM..
Basscyst is offline   Reply With Quote
Old 09-12-2007, 10:43 PM   PM User | #7
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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
BarrMan is offline   Reply With Quote
Old 09-12-2007, 11:41 PM   PM User | #8
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
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
Basscyst is offline   Reply With Quote
Old 09-13-2007, 12:11 AM   PM User | #9
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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!
BarrMan is offline   Reply With Quote
Old 09-13-2007, 12:46 AM   PM User | #10
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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!
BarrMan is offline   Reply With Quote
Old 09-13-2007, 12:59 AM   PM User | #11
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
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
Basscyst is offline   Reply With Quote
Users who have thanked Basscyst for this post:
BarrMan (09-15-2007)
Old 09-15-2007, 06:02 PM   PM User | #12
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
Forgot to thanks you
BarrMan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.