從this blog post來看,Greasemonkey並沒有提供jQuery使用的XHR對象,所以你必須包裝它提供的功能,並用自定義實現來設置jQuery AJAX。
我會在這裏引用的相關代碼:
// Wrapper for GM_xmlhttpRequest
function GM_XHR() {
this.type = null;
this.url = null;
this.async = null;
this.username = null;
this.password = null;
this.status = null;
this.headers = {};
this.readyState = null;
this.open = function(type, url, async, username, password) {
this.type = type ? type : null;
this.url = url ? url : null;
this.async = async ? async : null;
this.username = username ? username : null;
this.password = password ? password : null;
this.readyState = 1;
};
this.setRequestHeader = function(name, value) {
this.headers[name] = value;
};
this.abort = function() {
this.readyState = 0;
};
this.getResponseHeader = function(name) {
return this.headers[name];
};
this.send = function(data) {
this.data = data;
var that = this;
GM_xmlhttpRequest({
method: this.type,
url: this.url,
headers: this.headers,
data: this.data,
onload: function(rsp) {
// Populate wrapper object with returned data
for (k in rsp) {
that[k] = rsp[k];
}
},
onerror: function(rsp) {
for (k in rsp) {
that[k] = rsp[k];
}
},
onreadystatechange: function(rsp) {
for (k in rsp) {
that[k] = rsp[k];
}
}
});
};
};
而設置的jQuery的AJAX:
$.ajaxSetup({
xhr: function(){return new GM_XHR;}
});
這一步之後,.get()
和其他AJAX方法應很好地工作。
所以如果你去http://localhost/code.php,你看到了什麼?另外,我假設你是從http:// localhost /提供的頁面調用它的,對吧? –
你的JS控制檯說什麼?任何錯誤? – Quentin
你的服務器日誌呢?你能確認它正在接收請求嗎? – Quentin