我非常熟悉Angular的前端,它是新的HttpClient。我从来没有接触过后端,尤其是用Python编写的后端(这就是为什么我发现下面的任务很有挑战性)。我所要做的就是在Angular 6和Python之间实现双向通信( Python中的代码将字符串发送到Angular的Typescript中的代码,反之亦然- typeScript代码将字符串发送到用Python编写的代码中)。
在web上找不到任何教程(在堆栈溢出/msdn中)。Python中的后端是由另一家公司开发的,他们的需求类似于:
Python代码应为3-4行,如下所示
Import some_module
Socket(..)/pipe()
Send(some_string)/receive(some_string)有没有人能帮我准备一些样例代码,告诉我怎么做?
我猜在角度方面,我应该只发出一个HTTP请求,通过API向Python发送一个字符串:
const observable = this.http.post<string>(AppConfig.baseUrl + sendToPythonUrl + stringToSend, {}).pipe(share());
return observablePython是如何接收这个字符串的?
另外,Python如何与Angular服务通信并向其发送字符串?
发布于 2018-07-17 20:03:48
像这样使用HttpClient:
constructor(private httpClient: HttpClient) {
}
/**
* This method is use for send GET http Request to API.
* @param url - Additional request URL.
* @param body - params.
* @param options - Header(s) which will pass with particular request.
*/
get(url: string, options?: any): Observable<any> {
return this.httpClient.get(url, {options})
}Component.ts中的Subscribedata:
constructor(
private service: ServiceClass){}
getData(){
this.service.get('url',this.options).subscribe(resp => {
console.log(resp);
})
}https://stackoverflow.com/questions/51380533
复制相似问题