为什么Mozilla的XMLHttpRequest示例无法在Firefox 3中使用? [英] Why is this XMLHttpRequest sample from Mozilla is not working in Firefox 3?

查看:46
本文介绍了为什么Mozilla的XMLHttpRequest示例无法在Firefox 3中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Mozilla获取示例代码,该示例代码使用REST Web服务在Firefox 3.0.10下工作.以下代码在Firefox中不起作用,但在IE 8中起作用!

I'm trying to get the sample code from Mozilla that consumes a REST web service to work under Firefox 3.0.10. The following code does NOT work in Firefox but does in IE 8!

  1. 为什么这不起作用?
  2. IE 8是否支持XMLHttpRequest?我见过的大多数示例都使用ActiveX分配.我该怎么办?XMLHttpRequest似乎更加标准化.

示例:

var req = new XMLHttpRequest();
req.open('GET', 'http://localhost/myRESTfulService/resource', false);    // throws 'undefined' exception
req.send(null);
if(req.status == 0)
  dump(req.responseText);

open语句引发了描述为'undefined'的异常.这很奇怪,因为我分配了req对象,正在Firefox中运行它,并在调用open(它说它是'object'类型)之前检查以确保已定义它.

The open statement is throwing an exception with the description 'undefined'. This is strange as I allocate the req object, am running it in Firefox, and checked to make sure it is defined before calling open (which it says it is of type 'object').

我也尝试过这种异步版本,但是没有运气.

I've also tried the asynchronous version of this with no luck.

以下是我最近的代码:

EDIT 2: Below is my most recent code:

function createRequestObject() {
    if( window.XMLHttpRequest ) {
        return new XMLHttpRequest();
    }
    else if( window.ActiveXObject ) {
        return new ActiveXObject( "Microsoft.XMLHTTP" );
    }

    return null;
}

function handleResponse( req ) {
    document.writeln( "Handling response..." );   // NEVER GETS CALLED
    if( req.readyState == 0 ) {
        document.writeln( "UNITIALIZED" );
    }
    else if( req.readyState == 1 ) {
        document.writeln( "LOADING" );
    }
    else if( req.readyState == 2 ) {
        document.writeln( "LOADED" );
    }
    else if( req.readyState == 3 ) {
        document.writeln( "INTERACTIVE" ); 
    }
    else if( req.readyState == 4 ) {
        document.writeln( "COMPLETE" );
        if( req.status == 200 ) {
            document.writeln( "SUCCESS" );
        }
    }
}

document.writeln( "" );
var req = createRequestObject();

try {
    document.writeln( "Opening service..." );
    req.onreadystatechange = function() { handleResponse( req ); };
    req.open('POST', 'http://localhost/test/test2.txt', true);  // WORKS IN IE8 & NOT FIREFOX


    document.writeln( "Sending service request..." );
    req.send('');

    document.writeln( "Done" );
}
catch( err ) {
    document.writeln( "ERROR: " + err.description );
}

好吧,我在jQuery中对其进行了重新设计.jQuery在IE中效果很好,但是从Firefox运行时会抛出未定义".我再次检查并在Firefox中启用了启用JavaScript"-似乎在所有其他网页上都能正常工作.下面是jQuery代码:

EDIT 3: Alright, I reworked this in jQuery. jQuery works great in IE but it throws 'Undefined' when running from Firefox. I double checked and 'Enable JavaScript' is turned on in Firefox - seems to work fine in all other web pages. Below is the jQuery code:

function handleResponse( resp ) {
    alert( "Name: " + resp.Name );
    alert( "URL: " + resp.URL );
}

$(document).ready( function() {
    $("a").click( function(event) {

        try {
            $.get( "http://localhost/services/ezekielservices/configservice/ezekielservices.svc/test", 
                   "{}",
                   function(data) { handleResponse( data ); },
                   "json" );
        } 
        catch( err ) {
            alert("'$.get' threw an exception: " + err.description);
        }

        event.preventDefault();
    });
} );    // End 'ready' check

解决方案摘要:

好的,网络课程101.我的问题确实是跨域的.我正在查看未发布的站点(仅在文件系统上),这影响了已发布的服务.当我在同一域下发布我的网站时,它起作用了.

Alright, web lesson 101. My problem was indeed cross-domain. I was viewing my site unpublished (just on the file system) which was hitting a published service. When I published my site under the same domain it worked.

这也带来了IE和Firefox之间的重要区别.当IE遇到这种情况时,它会提示用户是否接受跨域调用.Firefox引发异常.尽管我很好,但有一个更具描述性的人会有所帮助.

Which also brings up an important distinction between IE and Firefox. When IE experiences this scenario, it prompts the user whether or not they accept the cross-domain call. Firefox throws an exception. While I'm fine with an exception, a more descriptive one would have been helpful.

感谢所有帮助过我的人.

Thanks for all those who helped me.

推荐答案

,除非' http://www.mozilla.org/'是此请求的来源域,由于相同的来源策略,因此无法正常工作

unless 'http://www.mozilla.org/' is the domain from which this request originates, this wont work because of same origin policy

修改:好的,状态为200,而不是0.

edit: Ok, a good status is 200, not 0.

请参见 http://dogself.com/telluriumTest/,然后单击"stackoverflow测试".使用您的代码并正常工作.

see http://dogself.com/telluriumTest/ and click on "stackoverflow test". its using your code and working.

具体来说,此代码:

function test(){
    var req = new XMLHttpRequest();
    req.open('GET', 'index2.htm', false);    
    req.send(null);
    if(req.status == 200)
    alert("got some stuff back:"+req.responseText);
}

这篇关于为什么Mozilla的XMLHttpRequest示例无法在Firefox 3中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆