Angular2,HTTP post,发送动态json
问题描述:
我需要从其他函数中获取postKey和postValue,它会发送 的密钥名和密钥值,然后发布它。 但我不能找到一种方法来发送字符串化增值经销商,而不是静态值Angular2,HTTP post,发送动态json
这样它不工作:
postRequest(postKey?: any, postValue? any){
return this._http.post(this._url, JSON.stringify({postKey: PostValue }))
.map(res => res.json());
}
只喜欢这工作(静态):
postRequest(post?: any){
return this._http.post(this._url, JSON.stringify({SomeKey: 'SomeValue' }))
.map(res => res.json());
}
答
它可以通过使用达到ES6
feature
postRequest(postKey?: any, postValue){
return this._http.post(this._url, postKey ? JSON.stringify({[postKey]: PostValue }: null))
.map(res => res.json());
}
答
解决它像th是:
return this._http.post(this._url, '{"' + postKey + '": "' + postValue + '"}')
我收到错误: 参数类型的 '{}' 是不能分配给类型的参数“(字符串|数)[]'。 '{}'类型中缺少属性'find'。 ERROR in [default] C:\ Users \ shay \ Desktop \ Angular2 \ cloud \ src \ app \ http_request.service.ts:54:91 ':'expected。 –
@ShayBinyat尝试'null'而不是'{}' –
仍然不好 - 它想要在[默认] C:\ Users \ shay \ Desktop \ Angular2 \ cloud \ src \ app \ http_request中出现空错误之前的“,”。 service.ts:57:104 ','预计。 ERROR in [default] C:\ Users \ shay \ Desktop \ Angular2 \ cloud \ src \ app \ http_request.service.ts:57:112 ':'expected。 –