2017-10-19 48 views
0

HTTP POST沒有得到任何響應。我在開發人員工具中看不到任何回覆。代碼中有什麼錯誤。請幫忙。如何從API獲取響應並將新記錄添加到列表中。Node.js使用SQLlite的Restfull API不能與Angular 2 HTTP post配合使用

強大的文本 //我的REST API代碼: //把這些語句定義的任何路由之前。

var express = require('express'); 
var sqlite3 = require('sqlite3').verbose(); 
var db = new sqlite3.Database('app_data/SocBillSys.db'); 
var bodyParser = require('body-parser'); 

var app=express(); 

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 
app.use(allowCrossDomain); 

//這允許任何域訪問API服務器。

function allowCrossDomain(req, res, next){ 
     res.setHeader('Access-Control-Allow-Origin', '*'); 
     res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, 
    PUT, PATCH, DELETE'); // If needed 
     res.setHeader('Access-Control-Allow-Headers', 'X-Requested-  With,contenttype'); // If needed 
     res.setHeader('Access-Control-Allow-Credentials', true); // If needed 
     next(); 
} 

//插入記錄

app.post('/db/society', function(req,res) 
{ 

    db.run("INSERT INTO society (name, type, address, city, state, pincode) VALUES (?,?,?,?,?,?)", 
     [ req.body.name, 
     req.body.type, 
     req.body.address, 
     req.body.city, 
     req.body.state, 
     req.body.pincode ] 
    , 
    function(error,response) 
    { 
     if (error == null) 
      res.status(202).json({"id": this.lastID}); 

     else 
     res.status(500).send(error.message); 
    }); 

}); 

app.listen(3000, function() { 
    console.log('NODE.JS WEB API SERVER is listening at port 3000...'); 
}); 

ANGULAR服務HTTP調用

import { Injectable } from '@angular/core'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { ISociety } from '../Society/Society'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 

@Injectable() 
export class SocietyService { 

    constructor(private _http: Http){} 

createSociety(society: ISociety): void{ 
     let headers = new Headers({'Content-Type': 'application/json' }); 
     let options = new RequestOptions({headers: headers}); 
     this._http.post('http://localhost:3000/db/society', JSON.stringify(society), options) 
      .map(this.handleResponse) 
      .catch(this.handleError);   
    } 



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

    private handleError (error: Response | any) { 
     console.error(error.message || error); 
     return Observable.throw(error.message || error); 
     } 


} 

回答

1
function allowCrossDomain(req, res, next){ 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, 
PUT, PATCH, DELETE'); // If needed 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type'); // If needed 
    res.setHeader('Access-Control-Allow-Credentials', true); // If needed 
    next(); 

}

+0

我沒有看到你的代碼等我的代碼有什麼區別比'X-Requested-With,Content-Type'但這不是問題。這只是我的副本問題。 –

+0

感謝Ayush提供您的解決方案。我第一次不明白你糾正了什麼,但後來我知道了。我感謝您的幫助 –