2017-08-08 50 views
0

我尝试发送此json到我的php服务器{"obj":{"r": "hello", "u": "[email protected]", "p": "123abc"}}angular2 http post变换。在_

当我发送,我的服务器收到{"obj":{"r": "hello", "u": "[email protected]_com", "p": "123abc"}},我不明白为什么。

这里是我的角度代码:

data = {"obj":{"r": "hello", "u": "[email protected]", "p": "123abc"}}; 

postMethod(data): Observable<any> { 
    let headers = new Headers(); 
    this.createAuthorizationHeader(headers); 
    console.log(JSON.stringify(data)); 
    return this._http.post(this.serverUrl, JSON.stringify(data), {headers: headers}) 
     .map((response:Response) => response.json()) 
     .catch(this.handleError); 
    } 

回答

0

你说你使用的是PHP服务器。

即使您发送POST请求,如果您发送纯JSON,你应该检查在PHP像原始输入数据流:

$stuff = file_get_contents("php://input");

有了你这个,你可以得到的数据然后解码数据,然后做你的事情。

+0

比你的男人是神 –

0

两个选项:

  1. 不要JSON.stringify在第二个参数的数据。 Angular将处理内容类型。
  2. 手动将内容类型标题设置为'application/json'。
+0

我试图不使用JSON.stringify,我还没有解决问题。 我已经将'application/x-www-form-urlencoded'的内容类型头改为'application/json',但我无法用我的php服务器处理它 –

+0

从它的声音来看,它是一个问题在PHP方面。 Angular自动处理POST请求中正文对象的JSON转换,并将内容类型设置为application/json。最有可能发生的是你的PHP端没有正确接收application/json类型。 –

+0

你是对的。这是一个PHP方面的错误。非常感谢! –