gpt4 book ai didi

javascript - AngularJS的http服务总是返回错误

转载 作者:行者123 更新时间:2023-11-29 20:11:28 24 4
gpt4 key购买 nike

我想知道为什么我的 AngularJS http 服务总是返回状态为 -1 的错误。

我有 PHP 代码并将其作为 localhost/ajax.php 运行。该文件可以从数据库中检索数据。所以php工作正常。检索到的数据如下:

[{"id":"1","Name":"Mark","Gender":"Male","City":"London"},  
{"id":"2","Name":"John","Gender":"Male","City":"Chenni"},
{"id":"3","Name":"Hint","Gender":"Male","City":"Singapore"},
{"id":"4","Name":"Sara","Gender":"Female","City":"Sydney"},
{"id":"5","Name":"Tom","Gender":"Male","City":"New York"},
{"id":"6","Name":"Pam","Gender":"Male","City":"Los Angeles"},
{"id":"7","Name":"Catherine","Gender":"Female","City":"Chicago"},
{"id":"8","Name":"Mary","Gender":"Femal","City":"Houston"},
{"id":"9","Name":"Mike","Gender":"Male","City":"Phoenix"},
{"id":"10","Name":"Rosie","Gender":"Female","City":"Manchestor"},
{"id":"11","Name":"Lim","Gender":"Male","City":"Singapore"},
{"id":"12","Name":"Tony","Gender":"Male","City":"Hong Kong"},
{"id":"13","Name":"Royce","Gender":"Male","City":"London"},
{"id":"14","Name":"Hitler","Gender":"Male","City":"Germany"},
{"id":"15","Name":"Tommy","Gender":"Male","City":"New Jersy"}]

此 PHP 文件是从我的 AngularJS http 服务调用的,但该调用始终返回状态为 -1 的错误。

我查看了所有资源,但没有任何线索。我的数据库是使用 Sql 在本地主机设置的。

我的疑问是可能出现的错误。我认为这些使 http 服务返回错误状态 -1。

我的代码如下:

Ajax.php

<?php
require 'connect.php';

$connect = connect();

// Get the data
$students = array();
$sql = "SELECT id, Name, Gender, City FROM tblStudents";

if($result = mysqli_query($connect,$sql))
{

$count = mysqli_num_rows($result);
$cr = 0;
while($row = mysqli_fetch_assoc($result))
{
$students[$cr]['id'] = $row['id'];
$students[$cr]['Name'] = $row['Name'];
$students[$cr]['Gender'] = $row['Gender'];
$students[$cr]['City'] = $row['City'];
$cr++;
}
}

$json = json_encode($students);
echo $json;
exit;

?>

连接.php

<?php
// db credentials
define('DB_HOST', 'localhost');
define('DB_USER','root');
define('DB_PASS','nyan');
define('DB_NAME','Students');

// Connect with the database.
function connect()
{
$connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);

if (mysqli_connect_errno($connect))
{
die("Failed to connect:" . mysqli_connect_error());
}

mysqli_set_charset($connect, "utf8");


return $connect;
}


?>

脚本.js

var app = angular.module("Demo", ["ngRoute"])
.config(function($routeProvider){
$routeProvider
.when("/home", {
templateUrl:"Templates/home.html",
controller:"homeController"
})
.when("/courses", {
templateUrl:"Templates/courses.html",
controller:"coursesController"
})
.when("/students", {
templateUrl:"Templates/students.html",
controller:"studentsController"
})
})
.controller("homeController", function($scope){
$scope.message = "Home Page";
})
.controller("coursesController", function($scope){
$scope.courses = ["C#", "VB.NET", "SQL Server", "ASP.NET"];
})
.controller("studentsController", function ($scope, $http) {
$scope.students;
$http({
method: 'GET',
url: 'api/ajax.php'
}).then(function (response) {
$scope.students = response.data;
}, function (response) {
console.log(response.data,response.status);
});
});

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html ng-app="Demo">
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="Scripts/angular.min.js" type="text/javascript"></script>
<script src="Scripts/angular-route.min.js" type="text/javascript"></script>
<script src="Scripts/Script.js" type="text/javascript"></script>
<link href="Styles.css" rel="stylesheet" type="text/css"/>
</head>
<body >
<table style="font-family: Arial">
<tr>
<td colspan="2" class="header">
<h1>
WebSite Header
</h1>
</td>
</tr>
<tr ng-controller="studentsController">
<td class="leftMenu">
<a href="#/home">Home</a>
<a href="#/courses">Courses</a>
<a href="#/students">Students</a>
</td>
<td class="mainContent">
<ng-view></ng-view>
</td>
</tr>
<tr>
<td colspan="2" class="footer">
<b>Website Footer</b>
</td>
</tr>
</table>
</body>
</html>

最佳答案

您在问题中指出您的脚本可能由于数据库查询而中断。

当您的查询花费很长时间时,可能会导致超时。响应代码 -1 通常意味着请求被中止,例如根据 Angular js 文档 https://docs.angularjs.org/api/ng/service/ 使用 config.timeout $http。

您可以尝试增加超时时间:

$http.get('ajax.php', {timeout: 5000, method: 'GET'});

在此示例中,它将设置为 5 秒。测试您的 ajax.php 以查看您的请求需要多长时间。

编辑:

还要确保您使用正确的 URL。在您的问题中,您使用不同的 URL 来测试 ajax 调用。

关于javascript - AngularJS的http服务总是返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40080518/

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