javascript - Test is URL is valid (server response = 200) -


i trying check if url provided user in form valid one, i.e. if server's response 200.

i want run check when user has typed in field , when loses focus. because of same origin policy, run check on server side:

client side code:

    // test url when input loses focus     angular.element(document).ready(function () {         $('#target_url').focusout(function() {             if ($('#target_url').hasclass('ng-dirty')){                 $http.post('/test-url', { target_url: scope.newjob.target_url }).success(function(response){                     if (response === 'valid url'){                         console.log('valid url');                     }else{                         console.log('url not valid');                     }                 });             };         });     }); 

server side:

// url testing route app.post('/test-url', function(req, res) {     request(req.body.target_url, function (err, response) {         if (response && response.statuscode === 200) {             res.send('valid url');         }else{             res.send('url not valid');         }     }) }); 

my problem here response not printed console when lose focus when start typing in inputs. doing wrong?

nb: using angular 1.1.5 why didnt use 'ng-focus'

short answer: need wrap focusout callback in $scope.$apply.

long answer: $.focusout jquery , runs outside of angular's digest cycle. therefore angular not end-of-digest signal trigget $http.post. when type in field, digest cycle completed , @ last $http.post fires.

suggested correction:

$('#target_url').focusout(function() {     $scope.$apply(function() {         ...existing code here...     }); }); 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -