将rest API调用从angular转换为jQuery



如果措辞笨拙,我很抱歉,但我必须使用jQuery进行一个rest API调用。我之前已经使用angularJS进行了调用,但对于这种情况,我不能使用它。我试着把它翻译成jQuery,但没有得到同样的结果。是我做错了什么,还是我遗漏了信息?我是jQuery的新手,所以我觉得我错过了一些关键的东西或误解了一些东西。

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

我失败的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调用

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

最新更新