...

Ajax returns a weird value to my textarea

BarrMan
09-12-2007, 11:39 AM
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:
<html><body><h1> HTTP/1.1 500 Server Error</h1></body></html>

Here's my script:
AJAX:
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:
<%@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
09-12-2007, 11:43 AM
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
09-12-2007, 04:10 PM
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.

Basscyst
09-12-2007, 07:28 PM
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.

BarrMan
09-12-2007, 09:34 PM
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!

Basscyst
09-12-2007, 10:16 PM
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.

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

BarrMan
09-12-2007, 10:43 PM
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:
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
Hello

Basscyst
09-12-2007, 11:41 PM
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.


//*******************************************************
// 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.


//*******************************************************
// 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:


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.

BarrMan
09-13-2007, 12:11 AM
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
09-13-2007, 12:46 AM
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! :thumbsup:

Basscyst
09-13-2007, 12:59 AM
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.

BarrMan
09-15-2007, 06:02 PM
Forgot to thanks you :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum