2016-07-28 20 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()。請檢查這可能是一個問題。謝謝 –