当前位置: 首页 > 知识库问答 >
问题:

Http.get请求返回错误,而与其他api样本工作正常

易星纬
2023-03-14

我试图从本地安装的moodle api在我的Nativescript移动应用程序中获得令牌。我已经确认api请求工作正常,以及请求代码。(当我使用json占位符数据时,返回预期的值)我在其他问题中阅读了关于CORS问题,并试图在我的本地服务器(apache2)上启用它,但迄今为止没有任何工作。

LoadModule头文件\模块libexec/apache2/mod头文件。因此,在httpd中。conf已启用,我添加了,

<Directory "/Users/lucy/Sites/">
AllowOverride All
Header set Access-Control-Allow-Origin "*" 
</Directory>

我已经测试了mod_头,以查看模块是否处于活动引用状态(在Mac OS X for CORS的apache中启用mod_头)

$ httpd -M |grep header

它回来了

AH00112: Warning: DocumentRoot [/usr/docs/dummy-host2.example.com] does not exist
headers_module (shared)

不确定是否与问题有关。我检查了moodle中的源文件(token.php),这个,

// Allow CORS requests.
header("Access-Control-Allow-Origin: *");

这应该允许从其他服务器访问(如果我理解正确的话)。任何帮助都将不胜感激。提前谢谢。

登录。服务ts

import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Login } from "../login/login";

@Injectable()
export class LoginService{

    constructor (private http: HttpClient) {}

    getToken(): Observable<Login[]> {    
        return this.http.get<Login[]>("http://mywebsite.dev.com/login/token.php/");
        // return this.http.get<Login[]>("https://jsonplaceholder.typicode.com/todos/1"); //test api working fine
    }
}

登录。ts

export class Login {
    token: string;
    privatetoken: string;
    // userId: number;
    // id: number;
    // title: string;
    // completed: boolean;
}

login.component.ts

import { Component, OnInit } from "@angular/core";
import { RadSideDrawer } from "nativescript-ui-sidedrawer";
import * as app from "tns-core-modules/application";

import { LoginService } from './login.service';
import { Login } from '../login/login';
import { Observable } from "rxjs";

@Component({
    selector: "Login",
    templateUrl: "./login.component.html"
})
export class LoginComponent implements OnInit {

    public token: Login[];    

    constructor(private loginService: LoginService) {
        // Use the component constructor to inject providers.
    }

    ngOnInit(): void {

        this.loginService.getToken().subscribe(token => {
            this.token = token;


        });
   }
}

错误消息

"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"status": 0,
"statusText": "Unknown Error",
"url": "http://mywebsite.dev.com/login/token.php/",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for http://mywebsite.dev.com/login/token.php/: 0 Unknown Error",
"error": {
"line": 147190,
"column": 38,
"sourceURL": "file:///app/vendor.js",
"originalStack": "ZoneAwareError(file:///app/vendor.js:147190:38)\nat file:///app/vendor.js:91152:37\nat UIApplicationMain([native code])\nat run(file:///app/vendor.js:85234:26)\nat file:///app/vendor.js:142544:26\nat file:///app/vendor.js:142445:38\nat file:///app/vendor.js:142425:26\nat file:///app/bundle.js:300:144\nat ./main.ts(file:///app/bundle.js:305:34)\nat __webpack_require__(<…>

共有1个答案

鄢修德
2023-03-14

我解决了这个问题。这不是CORS或标头问题,而是关于从本地应用程序连接localhost API的问题。我不得不使用ip地址而不是域名来连接它的API。更改get地址后,它现在工作正常。

return this.http.get<Login[]>("http://192.168.20.9/login/token.php");

无法从Android应用程序连接到localhost API此答案帮助我解决了问题。

 类似资料:
  • 我试图使用Gmail API将用户设置应用到Gmail帐户,但它不断返回错误400错误请求。 我可以看到错误代码在Gmail API控制台,它来自我的服务号,所以代码不可能是如此错误,但它让我发疯,只是不能找出什么是错误的。 如果有人能给我指出正确的方向,我会非常感激。

  • 在我的开发环境中,我有一个用户,我刚刚收到了以下作用域的 OAuth 令牌。 https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly 一切看起来都很好,我为用户存储了令牌。然后,

  • 我真的不知道我的java代码中的错误在哪里。我必须使用REST API登录Kofax Total Agility。为此,我尝试使用postman测试我的json是否正确构建。以下是我的登录JSON: 我得到了肯定的回答: 到目前为止,一切顺利。为此,我创建了模型: 对于响应: 这些类应该允许我构建 json。现在,我创建了一个方法,用于生成请求对象并期望响应对象。 当我调用这部分代码时,我注意到我

  • 问题内容: 这工作正常: 这将返回400 Bad Request(只是使用.ajax来支持错误处理的上述jQuery的重新格式)。 问题答案: 我认为您只需要再添加2个选项(和):

  • 我正在使用react.js应用程序调用API。我从AWS API网关收到关于CORS策略的错误: CORS策略阻止从来源“http://localhost:3000”访问位于“https://awsapigatewayid.execute-api.ap-south-1.amazonaws.com/staging/top10”的XMLHttpRequest:飞行前响应中的access-control

  • 我对API网关和Lambda非常陌生。我正在尝试将基于Node.js Express的API部署到lambda。我使用的是AWSlapes中的aws无服务器express示例。因此,我的大部分AWS配置都是自动为我创建的。 似乎我的API通过API网关正常工作。我的post和get方法工作正常。但是,我需要支持CORS。我的应用程序应该返回正确的CORS对选项请求的响应,但它在AWS上不起作用。