我有2個return語句:
return $http.post({
url: CHEAPWATCHER.config.domain + 'api/Authenticate',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: data
});
return $.ajax({
type: 'POST',
url: CHEAPWATCHER.config.domain + 'api/Authenticate',
data: data
}).done(function (result) {
console.log('logged successfuly');
}).fail(function (result) {
console.log('loging failed');
});
和使用API方法我background.js功能:
// REGISTER API COMMAND LISTENER
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (request, portInfo) {
// Globally accessible function to execure API calls
CHEAPWATCHER.executeApiCall = function (request, senderId) {
var originalRequestMethod = request.method;
//Dinamically call API method
CHEAPWATCHER.api[request.method](request, senderId).then(function (response) {
port.postMessage(response);
}, function (error) {
port.postMessage(error);
});
};
CHEAPWATCHER.executeApiCall(request, request.sender);
});
});
所以ofcourse當我跑步時我的瀏覽器擴展程序我評論他們中的一個,但問題是,他們都返回相同的對象類型jqXHR,如果我使用$ http.post它給了我這些錯誤:
Error in event handler for (unknown): TypeError: Cannot read property 'error' of null
和
POST chrome-extension://cmakfpdagfeiefcmmpmhjrtyhonmgnbi/background/[object%20Object] net::ERR_FILE_NOT_FOUND
,當我使用$就它的工作原理,我得到我所需要的令牌,但它給了我這個錯誤:
Error in event handler for (unknown): TypeError: Cannot read property 'error' of null
,之後我的分機不能繼續工作。昨天我問爲什麼$就使我的錯誤,我得到了答案,因爲我不能返回jqXHR類型,但如果我使用
return $http.post(CHEAPWATCHER.config.domain + 'api/Authenticate', data);
一切都只是罰款POST方法想,我給他grant_type =密碼和Content-Type應用程序/ x-www-form-urlencoded,這就是爲什麼我使用$ http.post([settings])結構。
有人可以請解釋我他們如何工作,爲什麼我得到這些錯誤?也許我錯誤地使用了post方法,或者我需要聲明更多我不知道的東西?
'$ http'是什麼? – Barmar
@Barmar很抱歉忘記提及我正在使用Angular。所以$ http服務是一個核心的Angular服務,通過瀏覽器的XMLHttpRequest對象或通過JSONP來促進與遠程HTTP服務器的通信。 –
您使用的是哪個版本的jQuery? – AhsanAyaz