I'm building a Nuxt.js app and I'm running into a CORS error. I already enables CORS on the server in which I am requesting resources from which resolved my original CORS error but now I'm seeing a new error, "It does not have HTTP ok status."
Here is the callout from the Nuxt.js app.
async executeCallout(context, payload) {
// Start loading...
let loadingId = context.dispatch('handleLoad', {
msg: 'Getting the thing',
method: 'executeCallout',
id: null
}, { root: true });
// Execute http request.
let url = context.getters.getCalloutUrl(payload.func);
let response = await this.$axios({
method: 'post',
headers: {
"Content-type": "application/json"
},
url: url,
data: payload.body
});
// Resolve loading...
context.dispatch('handleLoad', {
msg: null,
method: null,
id: loadingId
}, { root: true });
// Handle http response.
if (response.data.success) {
return response.data;
} else {
context.dispatch('handleError', {
msg: response.data.data,
origin: 'callouts.js',
method: 'executeCallout'
}, { root: true })
}
}
};
Here is the google cloud function that is handling the request.
const helper = require('./helper');
const Joi = require('joi');
exports.handler = async (req, res) => {
try {
// Set headers
res.set('Access-Control-Allow-Origin', '*');
if (req.method == 'OPTIONS') {
res.set('Access-Control-Allow-Methods', 'POST', 'OPTIONS');
res.set('Access-Control-Allow-Headers', 'Content-Type');
}
const payload = await schema.validate(req.body);
if (payload.error) {
res.status(400).json({
success: false,
data: payload.error
})
} else {
if (payload.value.product == 'quickbooks') {
let response = await helper.functionCall({ alias: payload.value.alias, function: 'intuit', payload: payload.value.payload });
res.status(response.success ? 200 : 400).json({
success: response.success ? true : false,
data: response.data
})
} else if (payload.value.product == 'distance-matrix') {
let response = await helper.functionCall({ alias: payload.value.alias, function: 'distance-matrix', payload: payload.value.payload });
res.status(response.success ? 200 : 400).json({
success: response.success ? true : false,
data: response.data
})
}
}
} catch (err) {
res.status(500).json({
success: false,
data: err
})
}
}
const schema = Joi.object({
alias: Joi.string().required(),
product: Joi.string(),
action: Joi.string(),
payload: Joi.object()
})
Response when trying attempting to send the request looks like this.