gpt4 book ai didi

javascript - AngularJS $http.get() 总是返回错误?

转载 作者:行者123 更新时间:2023-11-30 05:43:37 25 4
gpt4 key购买 nike

为什么下面的代码会运行没有状态的错误回调? Firefox 中的 Web Developer 报告状态代码 200。

有什么办法可以调试吗?

<meta charset="utf8">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>

<script type="text/javascript">

function RoomListCtrl($scope, $http) {

$scope.getRooms = function() {

$http({method: 'GET', url: 'http://code.angularjs.org'}).

success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
alert('succes');
}).

error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert('error');
});
}

$scope.submit = function() {

this.getRooms();
}

}

</script>

<body ng-controller="RoomListCtrl"><form ng-submit="submit()">
<input type="text" value="" ng-model="search" name="search" >
</form></body>

最佳答案

正如@robertklep 在上面指出的那样,除非该域明确允许,否则您不能向另一个域发出 ajax 请求。查看CORS他提供的链接。一种解决方法(假设您无法控制您从中请求数据的服务器的 CORS 策略)是使用 JSONP请求服务器是否支持。

关于javascript - AngularJS $http.get() 总是返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19347970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com