Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 后角5_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 后角5

Javascript 后角5,javascript,angular,typescript,Javascript,Angular,Typescript,我是angular的新手,在从后端的SpringREST获取数据时遇到了麻烦。 所以,场景是:我将从后端获取一个JSON字符串作为POST(JSON数据将作为POST重定向到我的站点托管链接),我需要捕获该JSON字符串并将其显示在UI上。 我不确定dataservice.ts中的post方法是否应该存在 我在谷歌上搜索了stackoverflow,发现下面的代码在我的场景中似乎不起作用: 组件。ts import { MyDataService } from './services/my-da

我是angular的新手,在从后端的SpringREST获取数据时遇到了麻烦。 所以,场景是:我将从后端获取一个
JSON
字符串作为
POST
(JSON数据将作为POST重定向到我的站点托管链接),我需要捕获该JSON字符串并将其显示在UI上。 我不确定dataservice.ts中的
post方法是否应该存在

我在谷歌上搜索了stackoverflow,发现下面的代码在我的场景中似乎不起作用:

组件。ts

import { MyDataService } from './services/my-data.service';
constructor(private posting: MyDataService
  ) {}

  ngOnInit() {
    this.posting.postMethod().subscribe(
    (response => { 
      console.log(response)
    }));
}
}
@Injectable()

export class MyDataService {

  constructor(private http: Http)
  { }


 postMethod(model: any  ) {
    return this.http.post("http ://", model)
      .map(res => res.json());
  }
}
数据服务.ts

import { MyDataService } from './services/my-data.service';
constructor(private posting: MyDataService
  ) {}

  ngOnInit() {
    this.posting.postMethod().subscribe(
    (response => { 
      console.log(response)
    }));
}
}
@Injectable()

export class MyDataService {

  constructor(private http: Http)
  { }


 postMethod(model: any  ) {
    return this.http.post("http ://", model)
      .map(res => res.json());
  }
}

正如错误所说,调用时需要将参数传递给服务

 this.posting.postMethod(model).subscribe(
    (response => { 
      console.log(response)
  }));

这是在subscribe方法中定义函数的正确方法:

ngOnInit() {
    this.posting.postMethod(model).subscribe(
    (response) => { 
      console.log(response)
    });
}

正如我在component.ts中看到的,您没有将模型作为参数传递。 您需要将模型作为参数传递

  this.posting.postMethod(anyData).subscribe(
    (response => { 
      console.log(response)
    }));

如果这不是问题所在,请向我们提供您收到的错误信息。

console中是否有任何错误?
.map(res=>res.json())不应该是needed@baao应该需要IMO for v5(如果您使用的是
http
而不是
httpClient
this.posting.postMethod()
将您的模型作为参数传递到此处。否,请阅读文章或任何角度更改日志。它从v5开始就被弃用,因为4.4版httpClient已经发布,应该改用@pardeepjain错误是“找不到名称模型”哈哈!你需要知道的。我已经提到了这里的问题不是downvoter,但是,除了方法中的param,这里有什么不同?response应该定义为一个函数,在括号内(response),而不是直接使用。不,您也可以直接使用它。它应该与括号一起使用/不与括号一起使用这将不会被视为一个函数。已经发布了这么多说明相同解决方案的答案,为什么要发布类似的答案。错误是:找不到名称“model”。虽然在postMethod中,我已经通过了model。我认为我们所有人的回答都是平行的@帕迪普