如果措辞笨拙,但我必须使用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