我正在尝试使用 JSONP 解析远程网站的 DOM 我认为问题是当我得到响应时它不是 JSON 所以延迟对象失败,但是我不能只是把原始数据带到那里并遍历dom节点以某种方式?$(文档).ready(函数() {
$('#submitUrl').on('click', function() {
var words = {};
var url = $('#url').val();
//send a JSONP request since its the only cross domain method available
var options = {
dataType: "JSONP",
type: 'GET',
url: url,
};
var arr = new Array();
var countArr = new Array();
var jqxhr = $.ajax(options)
.done(function(data) {
//this will always fail since jquery expects valid json response, try parsing in always pipe
})
.fail(function() {
})
.always(function(data) {
var fileDom = $(data);
//grab each element under the body dom element
//probably a filter method would work faster
fileDom.find('body *').each(function(index, value) {
if ($(this).text())
{
var txt = $(this).text();
//is this in the array already
if ($.inArray(txt, arr)) {
//get current ocunt
var c = parseInt(countArr[txt]);
c++;
countArr[txt] = c;
}
else {
countArr[txt] = 1;
}
}
});
});
});
});
html
<label>Input URL</label>
<input type="text" name="url" id="url" value=""/>
<input type="submit" id="submitUrl">