2016-07-28 38 views
0

如果措辞笨拙,但我必须使用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..."); 
     } 
    }); 
} 
+0

Ajax调用你得到一个错误,当您尝试使用jQuery的?像404,还是500? – KreepN

+0

通过“失败”控制台日志错误是什么样子,这就是调试开始的地方 – FrickeFresh

+0

@KreepN尝试使用jQuery时,控制台中没有出现任何错误。我非常确定,我拥有运行代码所需的所有jQuery库。 – Juan

回答

1

尝试制作这样

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

谢谢,但它似乎仍然没有工作。我尝试从Chrome进行调试,但它既不运行成功,也不运行错误功能。我把断点放在Ajax中,但是跳过了它们。 – Juan

+1

网址是否是正确的?我想他们可能是与URL的一些问题。 email.value有一些价值吗? –

+1

在您的原始代码中,您调用getBaseUrl()并将其添加到_req.url,所以ajax url应该与它有些相似。但在Ajax中,我们不会追加getBaseUrl()。请检查这可能是一个问题。谢谢 –