我试图通过代理解析一些html。对于其中一个,我通过jQuery
获取特定元素:
var site = 'http://www.kartabu.com/pl/index.php?filter=random'
var url = 'http://localhost/taboo.blue-world.pl/admin/proxy.php?url=' + encodeURIComponent(site)
$.ajax({
url : url,
type : 'GET',
dataType : 'html'
}).done(function(res) {
var div = $('<div></div>');
div.html(res);
var to_guess = div.find('.card_top_name').first().text().toLowerCase();
console.log(to_guess);
});
这是我的proxy.php文件:
$url = urldecode($_GET['url']);
$url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file system
echo file_get_contents($url); // You should probably use cURL. The concept is the same though
结果在控制台而不是控制台上打印。因为在源站点上有
pe�ny
。我该怎么解决?问题的根源在哪里?下面这一行也不能解决问题:
var to_guess = encodeURIComponent(div.find('.card_top_name').first().text().toLowerCase());
最佳答案
尝试在ajax中添加contentType: "application/x-www-form-urlencoded;charset=utf-8",
。这样地。
$.ajax({
url : url,
type : 'GET',
dataType : 'html',
contentType: "application/x-www-form-urlencoded;charset=utf-8",
}).done(function(res) {
var div = $('<div></div>');
div.html(res);
var to_guess = div.find('.card_top_name').first().text().toLowerCase();
console.log(to_guess);
});
这将导致独立于语言的结果。