最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Angular HTTP POST函数调用两次

IT培训 admin 3浏览 0评论

Angular HTTP POST函数调用两次

我在下面调用此服务并传递对象LocalDetails

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
import { share } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class OtherdataService {
  constructor(private http: Http) { }

  sendDBInfo(LocalDetails){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let body = JSON.stringify(LocalDetails);
    let options = new RequestOptions({ headers: headers });
    return this.http.post('http://hostname/api/requestuPatch/', body, options).map(result => result.json()).pipe(share());
  }
}

在node.js中,我试图执行一个shell脚本,因为我有参数,所以我将post对象转换为数组,并在exec中将它们传递给shellnode.js代码

app.post('/api/requestPatch/', function (req, res) {
    var data = JSON.parse(JSON.stringify(req.body));
    var shellargs = [];
    for(myKey in data){
        shellargs.push(data[myKey]);
    }

    var commacommand = 'bash /abc/bbc.sh ' + shellargs;
    var command = commacommand.split(',').join(' ');
    exec(command, function(error,stdout,stderr){
        if(error !== null){
            console.error();
        }
        else{
            res.end("success");
            console.log(stdout)
        }
    })
});

但是exec(command)调用了两次。你能帮忙吗

回答如下:

之所以发生这种情况,是因为您没有取消订阅可观察的项目,您需要这样做:

  destroy$ = new Subject<void>();
    ....
    ....
  return this.http.post('http://hostname/api/requestuPatch/', body, options).map(result => result.json()).pipe(takeUntil(this.destroy$), share());
  ... 
  ...

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$plete();
  }


Angular HTTP POST函数调用两次

我在下面调用此服务并传递对象LocalDetails

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
import { share } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class OtherdataService {
  constructor(private http: Http) { }

  sendDBInfo(LocalDetails){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let body = JSON.stringify(LocalDetails);
    let options = new RequestOptions({ headers: headers });
    return this.http.post('http://hostname/api/requestuPatch/', body, options).map(result => result.json()).pipe(share());
  }
}

在node.js中,我试图执行一个shell脚本,因为我有参数,所以我将post对象转换为数组,并在exec中将它们传递给shellnode.js代码

app.post('/api/requestPatch/', function (req, res) {
    var data = JSON.parse(JSON.stringify(req.body));
    var shellargs = [];
    for(myKey in data){
        shellargs.push(data[myKey]);
    }

    var commacommand = 'bash /abc/bbc.sh ' + shellargs;
    var command = commacommand.split(',').join(' ');
    exec(command, function(error,stdout,stderr){
        if(error !== null){
            console.error();
        }
        else{
            res.end("success");
            console.log(stdout)
        }
    })
});

但是exec(command)调用了两次。你能帮忙吗

回答如下:

之所以发生这种情况,是因为您没有取消订阅可观察的项目,您需要这样做:

  destroy$ = new Subject<void>();
    ....
    ....
  return this.http.post('http://hostname/api/requestuPatch/', body, options).map(result => result.json()).pipe(takeUntil(this.destroy$), share());
  ... 
  ...

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$plete();
  }


与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论