weixin_33749131 2013-12-20 14:35 采纳率: 0%
浏览 24

没有jQuery加载文本文件

I need to load the content of textfiles with Ajax - and I can't use Libraries like JQuery.

Everything is working fine, I receive the file and can work with its content. - In except of a javascript-warning in the Firefox-Console: "not well formed".

It seems that Firefox thinks I requested XML-content, which I didn't. So Firefox starts its XML-Parser and recognises that its not well-formed, noticeable with the warning in the console.

Here's the Ajax-Code I'm using:

function ajax(url, postData, callback) {
    var req = new XMLHttpRequest();
    req.open("GET",url,true);
     if (postData){
        req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    }
    req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 && req.status != 304) {
            return;
        }
        callback(req.response);
    }
    if (req.readyState == 4) return;
    req.send(postData);
}

What do I have to change, to tell Firefox that I just want a simple textfile (UTF8-encoded), and that it shouldn't parse it?

(Notice that Chrome doesn't print a Warning)

  • 写回答

1条回答 默认 最新

  • weixin_33701564 2013-12-20 15:35
    关注

    Use overrideMimeType to let the XMLHttpRequest know that it does not need to parse the file into an XML document

    req.overrideMimeType("text/plain; charset=utf-8")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题