M
M
Michael2016-12-29 19:13:21
Node.js
Michael, 2016-12-29 19:13:21

How to handle AJAX requests with koa2 and koa-router?

I am using koa-router in a project. I handle requests like this:

const Koa = require('koa'),
      router = require('koa-router')(),
      body = require('async-busboy'),
      app = new Koa();

app.use(router.routes());
app.use(router.allowedMethods());

router.post('/test', async(ctx) => {
    const data = await body(ctx.req);
});

Forms are processed successfully, but there are problems with native AJAX requests. I send like this:
const xhr = new XMLHttpRequest();
  xhr.open('POST', '/test', true);
  xhr.send({one: '12', two: 12});

Whatever headers I write, I see Unsupported content type in the console

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2016-12-29
@Arik

xhr.setRequestHeader('Content-type', 'application/json')
?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question