【问题标题】:when use .ashx adress got javascript error : myxmlhttprequest.open is not a function使用 .ashx 地址时出现 javascript 错误:myxmlhttprequest.open 不是函数
【发布时间】:2011-05-15 00:40:22
【问题描述】:

我无法访问 SayHello.ashx,我收到了这个 javascript 错误:

httpReq.open 不是函数。

但是当我使用 aspx 页面回答 xmlhttprequest 时,代码可以正常工作:

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

问题出在哪里?问题可能来自我的 .ashx 文件吗?

function callASHX() {
    var httpReq=XMLHttpRequest();        
    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;
}

【问题讨论】:

    标签: javascript xmlhttprequest ashx


    【解决方案1】:

    您没有分配XMLHttpRequest() 的返回对象。

    试试这个:

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

    【讨论】:

    • 谢谢你,但是当编译器到达这一行时我又遇到了同样的错误:httpReq.open("POST", "SayHello.ashx");
    • 注意,同样的错误。该页面用于连接到本地主机并完成,注意显示。我发现 Firefox 4 中的开发人员工具栏有错误
    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多