/ / Traduzione di una chiamata API rest da angular a jQuery: javascript, jquery, angularjs, rest

Traduzione di una chiamata API di riposo da angular a jQuery - javascript, jquery, angularjs, rest

Mi scuso se formulato in modo goffo, ma devo farlouna chiamata API rest utilizzando jQuery. Ho già fatto la chiamata usando angularJS prima, ma per questo caso non posso usarlo. Ho provato a tradurlo in jQuery ma non ottengo gli stessi risultati. C'è qualcosa che sto facendo di sbagliato o mi mancano delle informazioni? Sono abbastanza nuovo in jQuery quindi mi sento come se mi mancasse qualcosa di cruciale o qualcosa di frainteso.

Codice funzionante con 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);
}
});
};

Il mio codice jQuery in errore:

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

risposte:

1 per risposta № 1

Prova a fare una chiamata ajax come questa

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