/ / Rest API呼び出しをangularからjQueryに変換する-javascript、jquery、angularjs、rest

rest API呼び出しをangularからjQueryに変換する - javascript、jquery、angularjs、rest

ぎこちない言葉でお詫びしますが、私はしなければなりませんjQueryを使用したRESTAPI呼び出し。以前にangularJSを使用して呼び出しを行ったことがありますが、この場合は使用できません。それをjQueryに変換しようとしましたが、同じ結果が得られません。何か間違っていることや、情報が不足していることはありますか?私はjQueryにかなり慣れていないので、重要な何かを見逃している、または何かを誤解しているように感じます。

angleJSでの作業コード:

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

回答:

回答№1は1

このようにajax呼び出しを行ってみてください

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