/ / Traduzindo uma chamada de API rest de angular para jQuery - javascript, jquery, angularjs, rest

Traduzindo uma chamada de API de repouso de angular para jQuery - javascript, jquery, angularjs, rest

Desculpas se for formulado de maneira inadequada, mas tenho que fazeruma chamada de API restante usando jQuery. Já fiz a chamada usando o angularJS antes, mas neste caso não posso usar isso. Tentei traduzi-lo para jQuery, mas não estou obtendo os mesmos resultados. Há algo que estou fazendo de errado ou estou faltando alguma informação? Eu sou bastante novo no jQuery, então sinto que estou perdendo algo crucial ou algo mal compreendido.

Código de trabalho com angularJS:

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);
}
});
};

Meu código jQuery com falha:

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...");
}
});
}

Respostas:

1 para resposta № 1

Tente fazer uma chamada ajax como esta

var processCreate = function (email) {
var authHeaders = {};
authHeaders.Authorization = "Bearer " + "glsFromWebsite";

$.ajax({
url: "https://fakeurl.com/rest/v1/portal/user/" + email.value,
type: "POST",
cache: false,
dataType : "json",
contentType: "application/json; charset=utf-8",
headers: authHeaders,
success: function (data) {

//console.log(data);
if (data === "NOT FOUND") {
//code that runs when this case is true
} else {
//code that runs when this case is false
}
},
error: function (xhr) {
console.log(xhr);
}
});
}