Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Angular2 http POST正文作为null发送_Angular - Fatal编程技术网

Angular2 http POST正文作为null发送

Angular2 http POST正文作为null发送,angular,Angular,对于POST请求,我的服务器需要以下内容: { "contextId": 0, "role": "Test", "eng_reason": "string", "aspiration": "string", "perception": "string", "hobbies": [ { "hobbyId": 0, "name": "string", "selected": true, "contextId": 0

对于POST请求,我的服务器需要以下内容:

{
  "contextId": 0,
  "role": "Test",
  "eng_reason": "string",
  "aspiration": "string",
  "perception": "string",
  "hobbies": [
    {
      "hobbyId": 0,
      "name": "string",
      "selected": true,
      "contextId": 0
    }
  ],
  "interests": [
    {
      "interestId": 0,
      "name": "string",
      "selected": true,
      "contextId": 0
    }
  ],
  "skills": [
    {
      "skillId": 0,
      "name": "string",
      "selected": true,
      "contextId": 0
    }
  ],
  "connections": [

  ]
}
[
  {
    "contextId": 11,
    "role": null,
    "eng_reason": null,
    "aspiration": null,
    "perception": null,
    "hobbies": [],
    "interests": [],
    "skills": [],
    "connections": []
  }
]
我的服务具有以下功能:

  createContext(context: ContextlModel): Observable<ContextlModel[]>{

    let body=JSON.stringify(context)
    console.log(body)

    let headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.base_url + 'Context', {body} , options)
    .map(this.extractData)
    .catch(this.handleError);
  }
一切基本上都是空的。为什么会这样?为什么即使在JSON.stringify()之后也不能正确设置我的正文

更新:

  private extractData(res: Response) {
    let body = res.json();
    return body || { };
  }

如果我复制这个console.log(body)并通过我的Swagger API发布它,它是成功的,这意味着我的Angular POST请求中存在一些问题

你让事情变得比实际需要的更复杂了。Angular将负责在发送对象之前将其转换为JSON。您只需提供要发送的对象:

  createContext(context: ContextlModel): Observable<ContextlModel[]>{
    let headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.base_url + 'Context', context, options)
    .map(this.extractData)
    .catch(this.handleError);
  }
createContext(context:ContextlModel):可观察{
let headers=新的头({'Access Control Allow Origin':'*');
let options=newrequestoptions({headers:headers});
返回this.http.post(this.base_url+'Context',Context,options)
.map(此.extractData)
.接住(这个.把手错误);
}

PS——我经常发现使用Chrome的“开发工具网络”选项卡从浏览器的角度查看文章的确切内容很有用。它可以帮助我调试此类问题。

您服务器上的方法如何处理此请求?它在那里工作正常吗?在将该方法返回给客户端之前,您确定该方法的结果是正确的吗?那么
extractData
方法做什么呢?是的,如果我向Swagger发出相同的请求,它就会成功。我将用extractData()更新我的问题您是否尝试过将
let body=res.json()作为ContextModel[]?或者
让body=res.json()
?如果我没有错的话,我猜你指的是extractData()?但问题是POST本身没有将数据正确地发布到我的后端。是否可能不需要
正文
周围的括号?尝试以下操作:
返回this.http.post(this.base\u url+'Context',body,options)
。此外,您还可以在不使用JSON.stringify()的情况下尝试它。