问题
I have a service:
angular.module('services', [
])
.service('userService', function ($http, $q, auth, userFactory) {
var user = null;
if(!user){
userFactory.getUser2(auth.currentUser()).getValue(function(result){
//console.log(result);
user = result;
console.log("request");
});
}
this.getUser = function() {
//console.log(user);
return user;
}
})
;
This service calls a factory function:
userFactory.getUser2 = function(usr){
return{
getValue: function(callback){
$http({
method: 'POST',
url:'http://groep6api.herokuapp.com/user',
headers: {'Content-Type': 'application/x-www-form-urlencoded'
//'authentication': 'bearer' + $window.localStorage.getItem('eva-token')
},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : {username: usr}
}).success(function (result) {
//console.log("userfactory getUser:", result[0]);
callback(result[0]);
});
}
}
};
Now when I execute the code, I see this in my developer tools:
Where does this come from? The
console.log("request");
Gets only executed one time, but apparently there are 2 requests?
回答1:
In short, index is the page you're on.
If you open a page page without specifying an index.html page then the server will try to load an index document for that folder. When Chrome needs to inform you of an error
or console.log()
, it can not since it does not know the actual file name.
Hence (index)
来源:https://stackoverflow.com/questions/34009157/different-source-from-requests