When I pass a null value I get the backend function return, but when the value is not null, I get a bad request (400) error. See what I'm doing, what could be wrong?
Error:
POST link 400 (Bad Request)
Failed to load link : In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access. The response had HTTP status code 400.
My code:
Factory angularjs:
function ApiExterna($http, consts) {
function buscarCep(cep) { //cep é uma string
$http.post('http://localhost:3003/api/buscarCep', cep)
.then(resp => {
console.log(resp)
return null
}).catch(function (resp) {
)
}
return { buscarCep }
}
Backend route:
const express = require('express')
const auth = require('./auth')
module.exports = function(server) {
const protectedApi = express.Router()
server.use('/api', protectedApi)
const ApiService = require('../api/apiExterna/apiService')
protectedApi.post('/buscarCep', ApiService.buscarCep)
}
Backend service:
const bodyParser = require('body-parser')
const buscarCep = (req, res, next) => {
if (!req.body.cep) return res.send("hahahahah" + req.body)
res.send('welcome' + req.body)
}
module.exports = { buscarCep }
Any help? Thank you!