Handle Relative Image Paths ajax

不羁岁月 提交于 2019-12-24 01:48:11

问题


I'm developing a small Google chrome extension that screen scrapes a certain website. The problem is that because that website uses relative paths the links become broken and I get lots of errors like this:

GET chrome-extension://higopdenpioddpmobbehnbcadeenihic/html/images/ico_aAO.gif
GET chrome-extension://higopdenpioddpmobbehnbcadeenihic/html/images/ico_dAO.gif

Is there any way to avoid these errors?

The code I'm using for making the GET call is:

 $.ajax({
        url: searchUrl,
        type: "GET",
        data: {
            'pal': query
        },
        success: function() {
        },
        error: function() {
        }
    });

Thanks!


回答1:


The solution is to not use jQuery to parse the document. You can use jQuery.ajax if you want to, but do not use $ to parse the result.

Read the answer to Console shows error about Content Security policy and lots of failed GET requests to understand and solve your problem.

The answer strongly recommends to use vanilla JavaScript. If you want to use jQuery nevertheless, use the following DOM parsing method:

// responseText is a string, for example from calling jQuery.ajax
var doc = document.implementation.createHTMLDocument('');
doc.documentElement.innerHTML = responseText;
var $doc = $(doc);
// Enjoy the parsed document without errors!


来源:https://stackoverflow.com/questions/15174905/handle-relative-image-paths-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!