开发者

when use .ashx adress got javascript error : myxmlhttprequest.open is not a function

i cant access to SayHello.ashx and i got this javascript error :

httpReq.open is not a function.

but the code work fine when i use an aspx page for answer xmlhttprequest :

httpReq.open("POST", "SayHello.aspx");  

where is the problem? can the problem be from my .ashx file ?

function callASHX() {
    var httpReq=XMLHttpRequ开发者_JS百科est();        
    var x = document.getElementById('txtName').value; 
    var sendStr = "user_id=" + x;
    httpReq.open("POST", "SayHello.ashx");        
    httpReq.onreadystatechange = XMLHttpRequestCompleted;
    httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");      
    httpReq.send(sendStr);       
}

// initialize XMLHttpRequest object
function XMLHttpRequest() {
    var httpReq;
    try {
        // Opera 8.0+, Firefox, Safari
        httpReq = new XMLHttpRequest();
    }
    catch (e) {
        // IEBrowsers
        try {
            httpReq = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpReq = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                return false;
            }
        }
    }
    return httpReq;
}


You're not assigning the return object of XMLHttpRequest().

Try this:

function callASHX() 
{    
    var httpReq =  XMLHttpRequest();        
    ....
    if (httpReq)  //potentially this is 'false'!
    {
       httpReq.open("POST", "SayHello.ashx");  
       ....
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜