Jump to content

Problem with http.open related to AJAX


mb81

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]
Link to comment
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,,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.