Laique Ali
Laique Ali

Reputation: 57

Error: invalid json through ExtJs post method

I'm making an application having used ExtJs for front-end and for NodeJs (express) for back-end!

I've made a post request from ExtJs method, but it is throwing an error 400 Bad Request,

Here is my code for ExtJs method:

Ext.Ajax.request({
      url: '/controllers/test.js',
      method: 'POST',
      params: { "user": "test" },
      headers: { "Content-Type": "application/json" },
      success: function(res){ console.log("Success: " + res.responseText); },
      failure: function(err){ console.log("Error: " + err.responseText); },
      scope: this
    });

I've made some changes in URL like (home.html, test.json, test.js) but same problem is occurring when I call that method,

Please help me in this method, or give me any other solution for ExtJs call to NodeJs,

Any kind of help, will be useful for Me

Upvotes: 0

Views: 878

Answers (1)

hgoebl
hgoebl

Reputation: 13007

Try to send the request like this:

Ext.Ajax.request({
  url: '/test20090729',
  method: 'POST',
  jsonData: { "user": "test" },
  headers: { "Content-Type": "application/json" },
  success: function(res){ console.log("Success: " + res.responseText); },
  failure: function(err){ console.log("Error: " + err.responseText); },
  scope: this
});

And for me it seems that you haven't registered the route in express. Posting to controllers/test.js sounds a bit strange to me.

I've changed your URL to '/test20090729'. In your express app please add following code:

app.post('/test20090729', function (req, res) {
    console.log(req.body);
    res.send('I got your body!');
});

Another thing: Are you sure, you can reach your express application with relative URL? Is it delivering your ExtJS application statically? Then it's ok, if not, you might run into Same-Origin-Policy trouble but there are hundreds of questions here on SO.

Edit I removed the .end() after res.send(...) because this can cause problems especially when used with express.session.

Upvotes: 1

Related Questions