Reputation: 1694
I have a working angular project which connects to the real data using webapi2. currently i'm working on an express.js project which mocks the data for demo purposes, so instead of consuming the real webapi2, i'll consume this express project which always shows random data. The angular project calls a post api which expects a json object. But the call always returns error. but when called using postman it is working.
code:
angular component
onSubmit(valid: boolean = false): void {
...
this.service.save(request).subscribe(
result => {
console.log(result,"RHAILHYDRA");
...
},
error => {
console.log(error,"EHAILHYDRA");
...
})
}
angular service.ts
public save(param: any): Observable < any > {
let uri = "some url";
let payLoad = {
"someId": param.id,
"someComment": param.comment
};
let headers = new HttpHeaders({
'If-None-Match': param.tag
});
return this.http.post <any> (uri, payLoad, {
headers: headers
});
}
uri is always http://localhost:44309/demo/5b42/trade?valid=false
since its a mock, I didn't considered queries. so the api call will work with/without queries.
express.js code
var bodyParser = require('body-parser');
exports.register = function (app) {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/demo/:id/trade', function (req, res) {
res.status(200).json({
"validationMessages": [],
"overridable": true
});
});
};
When I click the submit button, it always goes to error "EHAILHYDRA".
but when I use postman, i'm getting expected result.
Any lead would be very helpful.
UPDATE #1:
When I click the submit, there's no call for this service in the inspect:network.
Upvotes: 1
Views: 589
Reputation: 2327
Maybe this issue comes when your header value is null or undefined. so remove header i mean 'If-None-Match': param.tag
part and try again.
public save(param: any): Observable < any > {
let uri = "some url";
let payLoad = {
"someId": param.id,
"someComment": param.comment
};
let httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
return this.http.post <any> (uri, payLoad, httpOptions);
}
Upvotes: 1
Reputation: 522
Try adding some check
on validationMessages
, whether it has some value or not.
Dont use the validationMessages
directly.
There can be a case where validationMessages
is undefined and you using that variable to call some predefined array functions.
Upvotes: 0