Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9064883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:22:02+00:00 2026-06-16T16:22:02+00:00

I have an ajax function in an angular service that checks a user’s credentials

  • 0

I have an ajax function in an angular service that checks a user’s credentials against a backend service. I’ve simplified the call quite a bit. The promise API works correctly when I use the $http service to issue the AJAX request:

function isOnline(){
    return $http.get(constants.dataUrl)
           .success(function(){return})
           .error(function(){return;});
}
function checkCredentials(){
    var online = isOnline();
    var checkCreds = online.then(function(){
        alert("Get succeeded");
    },
    function(response){
        alert("Get did not succeed");
    });
    return checkCreds;
}  

I see the functions defined in then called. When I use the jQuery ajax method though, the resolve and defer methods don’t seem to propagate and fire off the right methods in the online.then. The code below does not work:

function isOnline(){
    var defer = $q.defer();

    $.ajax({
      url: constants.dataUrl,
      dataType: 'json',
      beforeSend: function (xhr) {
        xhr.setRequestHeader('Authorization', basicAuthenticationToken());
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert("I am not online");
        defer.reject("I am not online");
      },
      success: function (data) {
        alert("I am online");
        defer.resolve(data);
      }
    });

    return defer.promise;
}
function checkCredentials(){
    var online = isOnline();
    var checkCreds = online.then(function(){
        alert("Get succeeded");
    },
    function(response){
        alert("Get did not succeed");
    });
    return checkCreds;
} 

Can I not use the promise API with the normal jQuery ajax methods? The reason I’d like to replace these calls has to do with a complicated PhoneGap scenario that doesn’t seem to work with Angular $http.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:22:03+00:00Added an answer on June 16, 2026 at 4:22 pm

    Angularjs needs to be notified of the changes that have happened outside of it. Since the ajax event is being handled by jquery you need to do an apply. Depending on where your function is (might be $scope or $rootScope)

    defer.reject("I am not online");
    $scope.$apply();
    
    ....
    defer.resolve(data);
    $scope.$apply();
    

    This will let angular know to reprocess itself. Here are the docs on it: http://docs.angularjs.org/api/ng.$rootScope.Scope#$apply

    You would use this feature anytime you have to write javascript outside of angular, but need it to wire up into it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery-ajax function that sends data to a php script and the
The problem is the following. We have lots of ajax call via $.ajax function.
I have written a simple jQuery.ajax function which loads a user control from the
I have two Ajax calls. I need to make sure that a JS function
i have an ajax function(with arguments) and i want to call it via html
I have simple AJAX function that uses jQuery to return an array of 300
I have an ajax function that loads the content of 4 checkboxes as follows:
I have an ajax function using jQuery that defines an error function to be
I have an ajax function and while caling that function the php code will
I have an ajax function that I am calling. While the Webserver is churning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.