【发布时间】:2018-11-12 22:20:17
【问题描述】:
我正在尝试向我的 php 文件发出 http 请求。但它返回给我以下错误
可能未处理的拒绝:{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null], "jsonpCallbackParam":"callback","url":"localhost/sample.php","headers":{"Accept":"application/json, text/plain, /"}}, "statusText":"","xhrStatus":"error"}
我的角度要求是
var some = angular.module("first_module", [])
.controller("first_controller", function($scope, $http, $log) {
$http.get('localhost/sample.php').then(function(response) {
$scope.students = response;
$log.info();
});
});
我的 PHP 文件是
<?php
$var2 = new stdClass();
$var2->name = 'john';
$var2->class = '7';
$var2->fee = '45000';
$var2->point = '5';
$response[0] = $var2;
$var2->name = 'ahmed';
$var2->class = '7';
$var2->fee = '55600';
$var2->point = '2';
$response[1] = $var2;
echo json_encode($response);
?>
我什至无法理解错误。在此先感谢
更新: 在为失败添加另一个功能并在控制台中打印日志时,我收到以下错误
var some = angular.module("first_module", [])
.controller("first_controller", function($scope, $http, $log) {
$http.get('localhost/sample.php').then(function(response) {
$scope.students = response;
$log.info();
}, function(err) {
console.log($log.info);
});
});
【问题讨论】:
-
我没有使用过 php 调用,但我认为你应该从 $http 中删除
localhost。可以分享一下控制台报错截图吗 -
感谢您的回复,但我在删除 localhost 时也收到同样的错误
-
分享控制台报错截图。
-
感谢@Shashank Vivek 的回复并用图片更新了它
-
-1 状态通常表示 CORS 问题。