在 FF & 中使用 javascript 从指定 url 加载 xml 的问题谷歌浏览器 [英] Problem in Loading xml from specified url using javascript in FF & Google Chrome

查看:21
本文介绍了在 FF & 中使用 javascript 从指定 url 加载 xml 的问题谷歌浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 javascript 从指定的 url 加载 xml 文件.这是我在做什么:

I need to load xml file from specified url using javascript. Here is what i am doing:

function GetXMLDoc() {
    var url = 'http://www.warm.fm/exports/NowOnAir.xml';
    var httpRequest = null;
    try {
        httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); 
    }
    catch (e) {
        try {
            httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); 
        }
        catch (e2) {
            try {
                httpRequest = new XMLHttpRequest(); 
            }
            catch (e3) { httpRequest = false; }
        }
    }
    if (httpRequest) {
        httpRequest.open('POST', url, false);
        httpRequest.setRequestHeader("Content-Type", "text/xml");
        httpRequest.send(null);
        alert(httpRequest.responseText);
    }
    else {
        alert(httpRequest);
    }

它在 IE 中完美运行,但在 FF 和 Google Chrome 中却没有.Firebug 向我显示以下错误:

It works perfectly in IE but it does not in FF and Google Chrome. Firebug shows me the following error:

未捕获的异常:[异常...组件返回失败代码:0x80004005(NS_ERROR_FAILURE)[nsIXMLHttpRequest.send]" nsresult:0x80004005(NS_ERROR_FAILURE)"位置:JS框架:: http://localhost:49697/XMLParser.js :: GetXMLDoc :: line 43" data: no]

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://localhost:49697/XMLParser.js :: GetXMLDoc :: line 43" data: no]

有没有人有答案可以帮助我解决问题?

Is there anyone who has the answer that will help me in solving the issue?

谢谢,莫欣

推荐答案

    httpRequest.open('POST', url, false);
    httpRequest.setRequestHeader("Content-Type", "text/xml");
    httpRequest.send(null);

这真的没有意义.您正在执行 POST 并声称您正在发送一个 XML 文件作为请求正文,但随后什么也没发送.

This doesn't really make sense. You're doing a POST and claiming that you're sending an XML file as the request body, but then sending nothing.

我建议你真的想做一个简单的 GET:

I suggest you really want to do a simple GET:

    httpRequest.open('GET', url, false);
    httpRequest.send();

当然,您必须从 www.warm.fm 上的文档中执行此操作以满足同源策略;localhost 不起作用.

Naturally you will have to do this from a document on www.warm.fm to satisfy the same origin policy; localhost won't work.

我会认真重新考虑请求的同步性(open...false).这是在获取文件时冻结浏览器,这对用户非常不利.带有 onreadystatechange 回调的异步请求几乎总是可取的.

And I would seriously reconsider the synchronousness of the request (open...false). This is freeze the browser whilst the file is fetched, which is pretty user-hostile. Asynchronous requests with an onreadystatechange callback are almost always preferable.

此外,跨浏览器 xmlhttprequest 的东西有点老式,首先尝试 ActiveXObject.Native XMLHttpRequest 通常是第一个.试试这个 IE6 后备代码:

Also, the cross-browser xmlhttprequest stuff is a bit old fashioned, and tries ActiveXObject first. Native XMLHttpRequest is usually the one to go for first. Try this IE6 fallback code instead:

if (!window.XMLHttpRequest && 'ActiveXObject' in window) {
    window.XMLHttpRequest= function() {
        return new ActiveXObject('MSXML2.XMLHttp');
    }
}

然后你就可以在任何浏览器上执行new XMLHttpRequest().

then you can just do new XMLHttpRequest() on any browser.

这篇关于在 FF & 中使用 javascript 从指定 url 加载 xml 的问题谷歌浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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