Jump to content

Archived

This topic is now archived and is closed to further replies.

mb81

Problem with http.open related to AJAX

Recommended Posts

I am working on a simple AJAX function, but I seem to be having trouble with the http.open in the following code and I'm not sure what's going wrong. It may be something as simple as a bad a file reference, but I have used both relative and absolute links and neither has worked.

I have verified that the http is an object. Any help would be appreciated.

variablepassed:
file: ./ajaxtoolbox/checkusername.php
args: ?username=test
sendtodivID= 'usernamechecktext'
turnoffdivID = ''

[code]
function createRequestObject() {
    var ro;
    if (window.XMLHttpRequest) {
        ro = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.ActiveXObject) {
        ro = new ActiveXObject("Msxml2.XMLHTTP");
    } else {
        ro = false;
    }
    return ro;
}

var http = createRequestObject();
function specifyReq(file,args,sendtodivID,turnoffdivID) {
    if (http) {
        http.open('GET',file+args);
    }
    http.onreadystatechange = function() {
        if (http.readyState==4) {
            document.getElementById(sendtodivID).innerHTML = http.responseText;
            if (document.getElementById(turnoffdivID)) {
                document.getElementById(turnoffdivID).innerHTML = "";
            }
        }
    };
}
[/code]

Share this post


Link to post
Share on other sites
Hi there,,

you forget to use the send method, >http.send(args);<
That one should work,,
[code]
<html>
<head>
<title>Testing,,</title>
</head>
<body>
<script>
var file="checkusername.php";
var args="?username=test";
var sendtodivID="usernamechecktext";
var turnoffdivID="";

function createRequestObject() {
    var ro;
    if (window.XMLHttpRequest) {
        ro = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.ActiveXObject) {
        ro = new ActiveXObject("Msxml2.XMLHTTP");
    } else {
        ro = false;
    }
    return ro;
}

var http = createRequestObject();
function specifyReq(file,args,sendtodivID,turnoffdivID) {
    if (http)
        {
        http.open('GET',file+args);
        }
    else
        {
        return false;
        }
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(args);

    http.onreadystatechange = function()
    {
    if (http.readyState==4)
        {
        var res = http.responseText;
//to debug:
alert(res)

        document.getElementById(sendtodivID).innerHTML = res;
        if (document.getElementById(turnoffdivID))
            {
            document.getElementById(turnoffdivID).innerHTML = "done";
            }
        }
    }
}
</script>
<div id='usernamechecktext'>testing,,</div>
<div id='turnoff'>need to log,,...</div>
<input type="button" value="click" onclick="specifyReq(file,args,sendtodivID,'turnoff')">
</body>
</html>
</body>
</html>
[/code]
I'm not sure about the get method+Ajax (I use $_Post), but I guess the get method needs also to use the 'send' method,,

Hoping it helps,,

l8tr,,

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.