【问题标题】:Chrome XMLHttpRequest POST encoding issueChrome XMLHttpRequest POST 编码问题
【发布时间】:2011-11-15 20:00:17
【问题描述】:

我用 XMLHttpRequest 发送文件,像这样:

self.xhr.open("POST", params.url + "?al=" + params.accessLevel + "&desc=" + params.desc + "&album_id=" + params.aid);

var boundary = "xxxxxxxxx";
self.xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
self.xhr.setRequestHeader("Cache-Control", "no-cache");
self.xhr.setRequestHeader("X-Requested-With", "ajx");

var body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name='"+(params.fieldName || 'file')+"'; filename='" + encodeURIComponent(params.file.name) + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += self.reader.result + "\r\n";
body += "--" + boundary + "--";

if(self.xhr.sendAsBinary) {
    // firefox
    //self.xhr.overrideMimeType("application/octet-stream; charset=utf-8");
    self.xhr.sendAsBinary(body);
} else {
    // chrome (W3C spec.)
    self.xhr.send(body);
}

在 Firefox 中它可以完美运行。看一下萤火虫截图:http://s8.postimage.org/4dw4gd5j7/Untitled.png

但是在 Chrome 中它看起来很糟糕:http://s8.postimage.org/h7yrng8cl/chrome.png

看起来像是编码问题。我尝试添加一个charset utf-8,但还是不行。

【问题讨论】:

  • 您的问题是数据设置不正确还是调试器文本混乱?后者看起来是浏览器错误。

标签: google-chrome xmlhttprequest fileapi


【解决方案1】:

不同的浏览器句柄不同。你会尝试 jquery $.ajax 吗?像jquery,YUI这样的javascript库应该可以消除浏览器的差异。

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多