討論 » 開發

gm_xmlhttprequest vs xmlhttprequest

§
發表於:2015-11-08

gm_xmlhttprequest vs xmlhttprequest

I've across something a bit weird.

GM_xmlhttpRequest({
    method: "GET",
    url: url,
    onload: function (xhr) {
        var data = eval(xhr.responseText);
        console.log(data);
    }
});

vs

var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.onload = function () {
    var data = eval(xhr.responseText);
    console.log(data);
};
xhr.send();

For all intensive purposes, these two functions should return the same response text.(Cross origin is not an issue.) However, when I check the console log, the response for GM_xmlhttpRequest is incorrect. It is near identical but not quite the same as the response I get form XMLHttpRequest.

Any ideas on what might be causing this?

woxxom管理員
§
發表於:2015-11-08

I guess different request headers are sent by default. Try checking it in the inspector in the network tab or use Fiddler app.

Jixun.Moe管理員
§
發表於:2015-11-08

Any example outputs? What is the url you have used for testing?

btw, I think is better to use JSON.parse than eval.

§
發表於:2015-11-09
編輯:2015-11-09

Try this: http://www.dm5.com/m129373/chapterfun.ashx?cid=129373&page=1

If you eval the response text it should give an array:

GM_xmlhttpRequest values:

http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/1_8081.png?cid=129373&key=b349856c835e4e849e6e5ecbf6e5e54e
http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/2_1555.png?cid=129373&key=b349856c835e4e849e6e5ecbf6e5e54e

XMLHttpRequest values:

http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/1_8081.png?cid=129373&key=dc6cdb99e41f6b698ff5a0ae1305a1db
http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/2_1555.png?cid=129373&key=dc6cdb99e41f6b698ff5a0ae1305a1db

notice how the "key" part of the strings are different.

Also if I use the GM_xmlhttpRequest method, it doesn't show up in the network tab.

woxxom管理員
§
發表於:2015-11-09

it doesn't show up in the network tab.

Use Fiddler.

§
發表於:2015-11-10
編輯:2015-11-10

I found the problem. No referer header is being sent. It seems that by default it GM_xmlhttpRequest doesn't send a referer header so I needed to specify.

Ty guys. Still don't get why I had to use fiddler just to see this.

發表回覆

登入以回復