Angular 4没有将数据传递给服务器?

时间:2018-01-18 18:07:57

标签: angular typescript angular-services angular-components

我在组件中使用服务打印数据。但是当我把它发布到后端时,它会变成空对象。使用Angular 2+将数据发送到api的正确方法是什么?

app.component.ts

this.searchService.getSearch(this.searchObj1).subscribe(searchObj => {
         // this.detailService.changeMessage(searchObj.data)
       consle.log(searchObj);
        });

app.service.ts

import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class SearchService {

  constructor(private http: Http) { }

  getSearch(searchObj:{}) {
    let body = JSON.stringify(searchObj);
    console.log('Fac',body);
    return this.http.post('/api/search',body).map(res => res.json());
  }

}

SERVER

    const express = require('express');
const router = express.Router();
const request = require('request');

// declare axios for making http requests
const axios = require('axios');
const API = 'https://jsonplaceholder.typicode.com';
    router.post('/search', (req, response) => {
      // Get posts from the mock api
      console.log('CLientReq',req.body);
       request.post('http://sla/service/getHistoricEvents',
       { json: true,
         body: req.body,
         proxy:'http://one.proxy.att.com:8080',
         headers: {
         "content-type": "application/json",
         "authorization": "Basic bTA5NDg1QGNvbXMuYXR0LmNvbTpjYnVzTVNAMjAxNw=="
        }
       },
       function(err, res, body) {
         response.status(200).json(body);
       }
       );
    });

0 个答案:

没有答案