我有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