gpt4 book ai didi

javascript - XMLHttpRequest 无法在 angularjs 中加载

转载 作者:行者123 更新时间:2023-11-28 19:25:16 26 4
gpt4 key购买 nike

我正在从中学习site .

我将其用于 Android 应用程序来获取 PHP 数据并显示应用程序。

当我在 AngularJS 中启动 Web 服务时,Google Chrome 在控制台中给出以下错误:

XMLHttpRequest cannot load http://192.168.1.10/android_connect/JsonReturn.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

这是我的 JavaScript 代码:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>function GetUsers($scope, $http) {
// this is where the JSON from api.php is consumed
$http.get('http://192.168.1.10/android_connect/JsonReturn.php').
success(function(data) {
// here the data from the api is assigned to a variable named users
$scope.users = data;
});
}</script>

然后我设置 HTML 代码:

<div ng-controller="GetUsers">

<table>
<thead><tr><th>ID</th><th>Name</th><th>Email</th></tr></thead>
<tbody>
<tr ng-repeat="user in users"><td>{{user.id}}</td><td>{{ user.name }}</td><td>{{user.email}}</td></tr>
</tbody>
</tfoot></tfoot>
</table>

</div>

我尝试了发布的所有可能的解决方案,但它似乎无法解决我的代码中的问题。

最佳答案

您正在尝试使用 XMLHttpRequest 发出跨域请求。为此,响应请求的 Web 服务器需要将 Access-Control-Allow-Origin 响应 header 设置为 *

关于javascript - XMLHttpRequest 无法在 angularjs 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000198/

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