Wrox Programmer Forums
|
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2007, 02:59 AM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to amit1me
Default problem with ajax


hi everyone,
i am new to ajax, just was trying a few things
the first time i send a request to the web page i dont get any response ,and for every request send from the same page the server gets the same values that i send the first time ,the values dont change after that
what could be the possible reason for this

regards
 
Old January 16th, 2007, 01:07 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hi amit,
try this

HTML
<html>
<head>
<script src="selectcustomer.js"></script>
</head><body><form>
Select a Customer:
<select name="customers" onchange="showCustomer(this.value)">
<option value="ALFKI">Alfreds Futterkiste
<option value="NORTS ">North/South
<option value="WOLZA">Wolski Zajazd
</select>
</form><p>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
</p></body>
</html> JavaScriptvar xmlHttp

function showCustomer(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getcustomer.asp"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHt tp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
} Server sidesql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
sql=sql & request.querystring("q")

set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open sql, conn

response.write("<table>")
do until rs.EOF
for each x in rs.Fields
response.write("<tr><td><b>" & x.name & "</b></td>")
response.write("<td>" & x.value & "</td></tr>")
next
rs.MoveNext
loop

response.write("</table>")

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old January 16th, 2007, 02:36 AM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to amit1me
Default


hi surendran ,thank you for the reply
some how i got the think working ,but now i have another query

it is about xmlHttp.onreadystatechange

when i use this implementation the statechange is called only once
        try
        {
        var url = "muster_ajax.php"
        var params = "user="+username+"&password="+password+"&case=Logi n";

            alert(url+"=="+params);
        xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xmlHttp.setRequestHeader("Content-length", params.length);
         xmlHttp.setRequestHeader("Connection", "close");
        //alert("before state change");
        xmlHttp.onreadystatechange = StateChanged();
        alert("after ready state changed");
             xmlHttp.send(params);
        }
        catch(err)
        {
            alert(err.name + "error_message"+ err.message);
        }


here is my StateChanged Function
function StateChanged()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200 || xmlHttp.status == 304)
        {
            alert("here");
            alert(xmlHttp.responseText);
            //return xmlHttp.responseText;
        }
    }
    else if (xmlHttp.readyState == 0 )
    {
        alert("no result");
    }
    else
    {
        alert(xmlHttp.readyState+"loading");
    }
}

this way the StateChanges is called only once


And when i use implementation 2
ie


try
        {
        var url = "muster_ajax.php"
        var params = "user="+username+"&password="+password+"&case=Logi n";

            alert(url+"=="+params);
        xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xmlHttp.setRequestHeader("Content-length", params.length);
         xmlHttp.setRequestHeader("Connection", "close");
        //alert("before state change");
        xmlHttp.onreadystatechange = function ()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200 || xmlHttp.status == 304)
        {
            alert("here");
            alert(xmlHttp.responseText);
            //return xmlHttp.responseText;
        }
    }
    else if (xmlHttp.readyState == 0 )
    {
        alert("no result");
    }
    else
    {
        alert(xmlHttp.readyState+"loading");
    }
};


        alert("after ready state changed");
             xmlHttp.send(params);
        }
        catch(err)
        {
            alert(err.name + "error_message"+ err.message);
        }


the thing works fine


what could be the reason for such behaiour





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with AJAX in Firefox geoko BOOK: Professional Ajax ISBN: 978-0-471-77778-6 2 June 27th, 2009 11:57 AM
Ajax Problem kalyanykk ASP.NET 3.5 Basics 3 November 14th, 2008 08:33 PM
Problem with ajax updatepanel alexmalayil ASP.NET 2.0 Professional 0 October 18th, 2007 05:07 AM
problem with ajax amit1me BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 September 28th, 2007 01:14 AM
Problem with Ajax angshujit ASP.NET 2.0 Basics 10 February 16th, 2007 03:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.