gpt4 book ai didi

Angular 2 无法在 XMLHttpRequest : Invalid URL 上执行打开

转载 作者:太空狗 更新时间:2023-10-29 16:54:47 26 4
gpt4 key购买 nike

我正在尝试调用一个服务,它在 DHC 中工作,但是当我尝试调用我的 angular 2 项目时,它崩溃了,方法请求是 POST,从有电子邮件和密码的 body 接收一个对象,响应是一个 token ,我将在整个项目中使用它进行授权。

这是我的代码。

import { Injectable } from '@angular/core';
import { Http, Response, Headers, Request ,RequestOptions ,RequestMethod } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';

@Injectable()
export class LoginService {
constructor(private http:Http) {
}

getToken(){
var baseUrl = "xxx.xxx.x.xxx:xxxx/project/v1/admin/login";
var headers = new Headers();
headers.append("Content-Type", 'application/json');
var options = new RequestOptions({ headers: headers });
var objeto = {'email': 'xxxxxx', 'pass':'xxxx' }
var body2 = JSON.stringify(objeto);
var xhr = new XMLHttpRequest();
return this.http
.post(baseUrl, body2, options)
.map((response: Response) => response.json())
.subscribe(
data => console.log('Success uploading the opinion '+ data, data),
error => console.error(`Error: ${error}`)
);
}
}

我尝试从 Angular 2 实现 XMLHttp Request 调用,但错误是一样的,我不知道我是否可以在 Angular 2 中使用它,这是方法

return Observable.fromPromise(new Promise((resolve, reject) => {
// let formData: any = new FormData();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.response));
} else {
reject(xhr.response);
}
}
}
xhr.open("POST", baseUrl, true);
xhr.send(body2);
}));

帮忙:(谢谢

最佳答案

如果是 192.168.. 类型的 url,你应该在它前面添加一个 http://https:// .您可能需要在服务器端启用 CORS 选项。

关于Angular 2 无法在 XMLHttpRequest : Invalid URL 上执行打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461174/

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