ERR_EMPTY_RESPONSE . Nodejs 6 , Angular2 , socket at server 1.6.0 at client 1.6.0 , environment centos 6.5
my app use nodeJs , angular2 , socket.io .
- Nodejs 6
- Angular 2
- socket at server 1.6.0 at client 1.6.0
- MAC OS is my environment
I try to make a chat room :
- add a message to a chat room.
- switch another chat room.
- switch back to first room. Then nothing to display.
I've got chrome console messages.
zone.js:1382 GET http:/X.X.X.X:YYYY/socket.io/?EIO=3&transport=polling&t=LYvYIgZ net::ERR_EMPTY_RESPONSE
My code run well on some computer , but I got error when run on some other computers . I don't know why . Thanks for help !
I have the same problem but only when use the post method, it's functioning correctly in get and get with id :/... zone.js:1382 OPTIONS http://localhost:3003/videos net::ERR_EMPTY_RESPONSE :3003/videos:1 XHR failed loading: POST "http://localhost:3003/videos".
I get the same error with Angular2 POST. Driving me crazy. I call an Express API. If I call the same Express API using Fiddler put a JSON string in the Body and make content type application / json it works just fine. But in Angular it just makes a call that blows up the API.
public AddProject (body : Object): Observable<any> {
let bodyString = JSON.stringify(body); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers });
return this.http.post(this.actionUrlnewProject, bodyString, options)
.map(response => response.json())
.catch(this.handleError);
}
@bofcarbon1 Did you solve your problem ? I got the same issue now and spend a day but still have not finished.
In my case, DO NOT set header : headers.append('Content-Type', 'application/json');
Use this: headers.append('Content-Type', 'application/x-www-form-urlencoded');
It depends on server config.
@huynhquocmy
I did get it to work. and here is an example of my API and the Observable call.
API code Server.js
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', "*");
//"http://webresumeproject-bofcarbon1.c9users.io:8080");
//"http://webresumemaintproject-bofcarbon1.c9users.io:8080");
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Headers', 'application/json');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
API code - Routes....
// Get resume personal data app.get("/api/resume/personal", function(req, res, next) { db.collection("wr_personal").find({}, function(err, docs) { if(err) return next(err); docs.each(function(err, doc) { if(err) { res.json(err); console.log("Error:",err); } if(doc) { personalJ = { "name" : doc.name, "title" : doc.title, "phone" : doc.phone }; res.json(personalJ); } else { res.json(personalJ); personalJ = ""; res.end(); } }); }); });
The web app Observable code.....
public GetPersonal = (): Observable<string> => {
return this.http.get(this.actionUrlPersonal)
.map((response: Response) => <string>response.json())
.catch(this.handleError);
}