tampermonkey的一个坑
关于Tampermonkey脚本开发时遇到的一个坑
GM_XmlhttpRequest
这个GM函数在wiki上是如此解释的:Make an xmlHttpRequest.
Property of details:
method one of GET, HEAD, POST
url the destination URL
headers ie. user-agent, referer, ... (some special headers are not supported by Safari and Android browsers)
data some string to send via a POST request
binary send the data string in binary mode
timeout a timeout in ms
context a property which will be added to the response object
responseType one of arraybuffer, blob, json
overrideMimeType a MIME type for the request
anonymous don't send cookies with the requests (please see the fetch notes)
fetch (beta) use a fetch instead of a xhr request
(at Chrome this causes xhr.abort, details.timeout and xhr.onprogress to not work and makes >xhr.onreadystatechange receive only readyState 4 events)
username a username for authentication
password a passwordonload, onerror, onreadystatechange, onprogress, onloadstart, ontimeout
在这个地方,约定了Data参数,与ajax的不同,是使用string字符串的形式,详细表述在网上也有一些,但是问题不在这里
比较坑的是,使用post方法传送数据时,一直出现400错误,经过反复排查,才发现headers首部设置不能随便添加值,headers的字段会对请求发生干扰,由于chrome环境没法捕获数据包,因此不清楚到底是哪里发生了错误。
解决这个错误之后,又发现,不设置headers字段时,服务器上会接收不到参数
必须设置headers参数为
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
至此,一个完整的post请求才算正常
高高手,你的直播间怎么被封了,脚本好像也失效了