如果措辭笨拙,但我必須使用jQuery進行其他API調用時抱有歉意。我之前已經使用angularJS進行過調用,但對於這種情況,我無法使用它。我試着將它翻譯成jQuery,但我沒有得到相同的結果。有什麼我做錯了或我缺少信息?我對jQuery相當陌生,所以我覺得我錯過了某些重要或誤解的東西。與angularJS將其餘的API調用從角度轉換爲jQuery
工作代碼:
var req = {
method: 'POST',
url: 'https://fakeurl.com/rest/v1/portal/user/' + $scope.email.value,
headers:{
'Content-Type': 'application/json',
'Header_1': 'Yes',
'x-access-token': 'glsFromWebsite' //$scope.authInfo.token
}
};
restCall($http, req).then(function (res) {
// check for error even though 200 response
if (res.error) {
console.error("Error reported...");
} else {
` //enter success code here
}
});
var restCall = function(http, req) {
var _url = getBaseUrl() + req.url;
req.url = _url;
return new Promise(function(fulfill, reject) {
try {
http(req).then(function (res) {
// check for error even though 200 response
if (res.data.error) {
if (res.data.error === '601') {
console.error('Token is invalid or has expired');
} else {
console.error("Error from end point: " + res.data.error);
}
}
fulfill(res.data);
}, function(err) {
console.error('Error calling rest endpoint',err);
reject();
});
} catch (ex) {
console.error('Exception calling rest endpoint',ex);
reject(ex);
}
});
};
我失敗的jQuery代碼:
var processCreate = function (email) {
$.ajax({
url: 'https://fakeurl.com/rest/v1/portal/user/' + email.value,
type: 'POST',
headers: {
'Content-Type': 'application/json',
'Header_1': 'Yes',
'x-access-token': 'glsFromWebsite' //$scope.authInfo.token
},
success: function (res, a, b) {
if (res === 'NOT FOUND') {
//code that runs when this case is true
} else {
//code that runs when this case is false
}
},
error: function() {
console.error("Error...");
}
});
}
Ajax調用你得到一個錯誤,當您嘗試使用jQuery的?像404,還是500? – KreepN
通過「失敗」控制檯日誌錯誤是什麼樣子,這就是調試開始的地方 – FrickeFresh
@KreepN嘗試使用jQuery時,控制檯中沒有出現任何錯誤。我非常確定,我擁有運行代碼所需的所有jQuery庫。 – Juan