Mar*_*una 23 xmlhttprequest jwt typescript angular
我正在尝试调用服务,它可以在DHC中运行,但是当我尝试调用我的angular 2项目时,它会崩溃,方法请求是POST,从具有电子邮件和密码的正文接收对象,以及响应是一个令牌,我将在整个项目中用于授权.
这是我的代码.
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}`)
);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试从角度2实现XMLHttp请求调用,但错误是一样的,我不知道我是否可以在角度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);
}));
Run Code Online (Sandbox Code Playgroud)
Eyo*_*elD 17
就我而言,我能够通过固定的URL,从客户端,而不是从失踪提到的服务器端为echonax我的http请求来解决它http://的localhost.
根据我的要求,我的问题是 this.http.get(this.domain + '/api/prod/' + id).map(res => res.json())
我/从前面错过了一个api.它应该是/api
| 归档时间: |
|
| 查看次数: |
43996 次 |
| 最近记录: |