我試圖發送一個post請求時收到404錯誤,我不知道爲什麼。還得到'可能未處理的拒絕'錯誤。我對Angular很陌生,所以任何提示將不勝感激。我看過的文件過來,發現周圍$ http.post請求的結構的各種其他信息,但到目前爲止,我不能把它應用到我想要在這裏做:
NG-點擊從我的HTML文件:在我的JS文件
<button ng-click="addToFavorites(searchResults.response.data)" class="btn btn-success" type="button" name="addToFavorites">Add to Favorites</button>
方法調用
addToFavorites: function(movie) {
var newMovie = isNewMovie(movie); // verify if movie has already been favorited
if (newMovie) { // add to favoriteMovies if it's a new movie
console.log(movie);
// favoriteMovies.push(movie);
$http.post('/m', movie).then(function(response) {
console.log(response.data);
});
// .then(function(response) {
// console.log(response);
// }).catch(function(err) {
// console.log('error:', err);
// });
} else { // alert user if it's already been favorited
alert('This movie is already in your list of favorites.');
}
} // end addToFavorites()
全部JS文件:
var pmdbApp = angular.module('pmdbApp', []);
pmdbApp.controller('InputController', ['$scope', 'MovieService', function($scope, MovieService) {
console.log('InputController loaded');
$scope.title = ''; // data-bound to user input field
// $scope.searchForm = MovieService.searchForm;
$scope.searchOMDB = MovieService.searchOMDB; // data-bound to user button click
// reference to searchResults object
// object contains the OMDB response as a property
$scope.searchResults = MovieService.searchResults;
$scope.getPoster = MovieService.getPoster; // bound to 'Search OMDB' button
$scope.addToFavorites = MovieService.addToFavorites; // bound to 'Add to Favorites' button
}]); // end 'InputController'
pmdbApp.controller('OutputController', ['$scope', 'MovieService', function($scope, MovieService) {
console.log('OutputController loaded');
$scope.movieService = MovieService;
}]); // end 'OutputController'
pmdbApp.factory('MovieService', ['$http', function($http) {
// searchResults object will be used to store response from the OMDB API
var searchResults = {};
/*var searchForm = {
title: '',
searchResults: {}
};*/
var favoriteMovies = [];
function isNewMovie(movie) {
for (var i = 0; i < favoriteMovies.length; i++) {
if (movie.imdbID === favoriteMovies[i].imdbID) {
return false;
}
}
return true;
}
// var saveToDatabase = function(movie) {
// console.log('got here with movie', movie);
// $http.post('/movies', movie).then(function(response) {
// console.log('response');
// });
// }
// public information
return {
favoriteMovies: favoriteMovies,
searchResults: searchResults, // pass an object referece
// searchForm: searchForm,
searchOMDB: function(title) {
$http.get('http://www.omdbapi.com/?t=' + title).then(function(response) {
console.log(response);
if (response.data.Error) { // alert user if no movie matches search results
alert('Movie not found!');
} else { // otherwise store response as an object property
searchResults.response = response;
}
}); // end $http.get
}, // end searchOMDB()
addToFavorites: function(movie) {
var newMovie = isNewMovie(movie); // verify if movie has already been favorited
if (newMovie) { // add to favoriteMovies if it's a new movie
console.log(movie);
// favoriteMovies.push(movie);
$http.post('/m', movie).then(function(response) {
console.log(response.data);
});
// .then(function(response) {
// console.log(response);
// }).catch(function(err) {
// console.log('error:', err);
// });
} else { // alert user if it's already been favorited
alert('This movie is already in your list of favorites.');
}
} // end addToFavorites()
}; // end return
}]); // end 'MovieService'
每當我收到404時,都會使用Chrome的網絡調試選項卡清除。如果你有服務器的日誌,你的服務器日誌也會有幫助。 – kontiki
404表示您請求的網址不存在。 – Sorikairo
如上所述,該URL不存在或者您嘗試的URL不正確。通過在任何瀏覽器或任何http客戶端工具中點擊完整的URL來檢查URL是否可用。它會幫助你確定問題 – CrazyMac