从ajax函数返回responseText

时间:2011-10-11 17:29:04

标签: ajax responsetext

参见我写的以下函数 代码是接收一个url并在post / get方法中向该页面发送数据。该函数从目标页面返回responseText

function $send(url,data,method)
{
 if(window.XMLHttpRequest)
var connect = new XMLHttpRequest();
    else 
    var connect = newActiveXObject("Microsoft.XMLHTTP");
    if(connect)
    {

        method = method.toUpperCase();

        if(method == 'GET')
        {
            url += '?'+data;
            connect.open(method,url,true);
            connect.send();
        }
        else
        {
            connect.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            connect.open(method,url,true);
            connect.send(data);
        }

        connect.onreadystatechange = function()
        {
            alert(connect.readyState);
            if(connect.readyState==4 &&connect.status == 200)
            {

                return connect.responseText ;


            }


        }

        return  "FALSE";

    }
    else 
    {
        alert("$sendData() is not supported by your browser\n\nTRY ANOTHER BROWSER!!!");
        return "FALSE";
    }
}

由于readstate最初为0并最终更改,因此我的函数总是返回FALSE。有没有办法返回responseText

0 个答案:

没有答案