简体   繁体   English

检索 Post Request 响应并存储到 angular 8 中的变量中

[英]Retrieving the Post Request response and storing into a variable in angular 8

I am using the Face Recognition and Face Detection api by lambda and am supposed to get a response from the api using post.我正在使用 lambda 的人脸识别和人脸检测api 并且应该使用帖子得到 api 的响应。 Usually for getting responses I use get while for sending responses I use post.通常我使用 get 来获取回复,而我使用 post 来发送回复。 How do I get this response using post so I can tell whether the image has been recognized or not?如何使用 post 获得此响应,以便判断图像是否已被识别? My code is below:我的代码如下:

Node.js file Node.js 文件

facedetAPIRoutes.route("/").post(function (req, res){
    let imageUrl = req.body.url;
    myFaceDetAPI.recognizeImg(imageUrl);
});

Service file服务文件

sendImage(imgUrl){
    console.log(imgUrl);
    const obj = {
      url: imgUrl
    };    

    this.http.post(`${this.uri}`, obj)
      .subscribe(res => {
        console.log("this is res: " + res);
        console.log('image sent to api done');
      });
  }

Component file组件文件

public handleImage(webcamImage: WebcamImage): void {
    console.info("received webcam image", webcamImage);
    this.webcamImage = webcamImage;
    console.log("this is: " + this.webcamImage.imageAsDataUrl);
    //this.fda.sendImage(this.webcamImage.imageAsDataUrl);
    this.fda.sendImage("http://localhost:4000/uploads/1570563000257-arnold4.jpg");
  }

Hellow.你好。 You can use STATUS CODE of response.您可以使用响应的状态码。 200 status code is success status for operation. 200 状态码为操作成功状态。 or if error it sent you status code 4xx.或者如果错误,它会向您发送状态码 4xx。 You can read more about status code https://en.wikipedia.org/wiki/List_of_HTTP_status_codes您可以阅读有关状态码https://en.wikipedia.org/wiki/List_of_HTTP_status_codes的更多信息

Apparently it was a problem in my backend file.显然这是我的后端文件中的一个问题。 For this I simply used res.status.为此,我只是使用了 res.status。 That way I was able to use subscribe in my service file.这样我就可以在我的服务文件中使用订阅。

Node.js Node.js

facedetAPIRoutes.route("/").post(function (req, res){
    let imageUrl = req.body.url;
    res.status(200).json(myFaceDetAPI.recognizeImg(imageUrl));
});

Service File服务文件

 sendImage(imgUrl){
    console.log(imgUrl);
    const obj = {
      url: imgUrl
    };    

    this.http.post(`${this.uri}`, obj)
      .subscribe(res => {
        console.log("this is res: " + JSON.stringify(res));
        console.log('image sent to api done');
      });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM