public deptpostdbServer() {
let headers = new Headers();
headers.append('application/x-www-form-urlencoded', 'Content-Type');
headers.append('Access-Control-Allow-Methods', 'POST');
headers.append('Access-Control-Allow-Origin', '*');
return this.http
.post('http://172.16.0.125:8090/buyR/api/saveDeptInfo/150/Software', headers)
.map(res => res.json());
public datapostDisplay() {
this.deptpost = [];
this.sitehttpservice.deptpostdbServer().subscribe((res) => {
this.deptpost.push(res);
});
ERROR SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.Body.json (body.ts:36)
at MapSubscriber.eval [as project] (table.service.ts:77)
at MapSubscriber._next (map.ts:75)
at MapSubscriber.Subscriber.next (Subscriber.ts:95)
at XMLHttpRequest.onLoad (xhr_backend.ts:104)
at ZoneDelegate.invokeTask (zone.js:424)
at Object.onInvokeTask (ng_zone.ts:253)
at ZoneDelegate.invokeTask (zone.js:423)
at Zone.runTask (zone.js:191)
defaultErrorLogger @ errors.ts:42
ErrorHandler.handleError @ error_handler.ts:69
Post service
<div class="form-group" >
First: <input type="text" [(ngModel)]="data.DeptCd"
placeholder="text"/>
Last: <input type="text" [(ngModel)]="data.Desc"
placeholder="name"/>
<button (click)="datapostDisplay()">Save</button>
</div>
Angular2 Post服务是JSON解析错误。我已经传递了正确的参数。我可以使用 API 从数据库中获取。从方法我可以调用 API 使用来自文本框的帖子我无法发送帖子方法我收到 json 解析错误。
在您的帖子请求中,我看到了您的标题,但没有看到任何"参数"。您实际上是在发送一个空的发布请求。只需在请求中添加 2 个字段值,这应该可以解决您的错误,除非您的远程服务器脚本中存在其他错误。您的发布请求应如下所示:
let headers = new Headers({ 'Content-Type': 'application/json' });
let data = { postFieldName : postValue, postFieldName2 : postValue2 };
let params = JSON.stringify(data);
return this.http.post(URL, params, headers)
.map(this.extractData)
.catch(this.handleError);
然后,创建一个将处理 http 响应的 extractData 方法。如果错误仍然存在,则可以在发送 post 请求之前检查您的数据是否未定义。
查看位于此处的文档 ›