我正在使用whateverorigin.org 进行跨域调用并让html 重新显示在div 中。但有些非英语字符很奇怪(有点方形)。以下是我的代码。
$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/x-www-form-urlencoded; charset=utf-8"});
$.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('https://www.testserver/index.html') + '&callback=?', function(data){
$("div#test").html(data.contents);
});
如何取回正确的字符。
最佳答案
您正在执行 JSONP 请求,该请求是通过添加 <script>
来实现的元素指向当前页面并指向远程内容,而不是使用普通的 XMLHttpRequest。
XHR/JSON 响应是使用 UTF-8 解码的,<script>
使用浏览器认为当前页面的编码是什么来解释 s。因此,如果您没有指定,请使用 <meta charset="UTF-8">
或Content-Type: text/html;charset=utf-8
,那么浏览器将不得不猜测。如果它猜测 UTF-8 以外的内容(很有可能),那么包含的脚本将被错误解码。
关于javascript - 跨域返回非unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562872/