Having some trouble getting express to respond properly to my jquery ajax request. The actual posting is working fine, but no matter what I try I cant seem to actually get a data response from my app that I can use. At first it was just posting and hanging constantly, and like a minute later it would respond with an alert that said something like "XML Document loaded" (have no idea where it was coming from) -- Anyways, now its giving me

SyntaxError: Unexpected token ILLEGAL at parse (native) at IncomingMessage.

In my express App, I have:

    app.post('/save', function(req, res) {
      console.log(req.body.objectData);
      res.contentType('json');
      res.send({ some: 'json' });
    });

and in my jquery:

  $.ajax({
    url: "/save",
    type: "POST",
    dataType: "json",
    data: {objectData: someObject},
    contentType: "application/json",
    cache: false,
    timeout: 5000,
    complete: function() {
      //called when complete
      console.log('process complete');
    },

    success: function(data) {
      console.log(data);
      console.log('process sucess');
   },

    error: function() {
      console.log('process error');
    },
  });

As always any help would be much appreciated! thanks

link|flag

63% accept rate
The node tag is not for node.js – Raynos 31 mins ago

1 Answer

jQuery turns data into a queryString. So you need to extract it from req.url

require('url').parse(req.url, true).query should contain your data.

The req.body contains form elements by "name" I believe.

link|flag

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.