2

I have the following in myserver/index.js (which I run with node):

server.configure(function() {
  server.use(express.methodOverride());
  server.use(express.bodyParser());
  server.use(server.router);
  server.use(express.static(__dirname + '/public'));
  server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

server.post('/signup/submit', function(req, res) {
  console.log(req);
  res.send(req.body);
});

server.listen(process.env.PORT || 12345, function() {
  console.log('Server listening');
});

I have the following in the <script> section of my myserver/public/signup/index.html file

function submit() {
  $.ajax({
    url: "submit",
    type: "POST",
    contentType: "application/xml",
    data: '<?xml version="1.0"?><user>John Smith</user>',
    dataType: "xml",
    success: function(data, textStatus, jqXHR) { alert(data); },
    error: function(jqXHR, textStatus, errorThrown) { alert(errorThrown); }
});

When submit() gets called, an alert pops up saying "Error: Invalid XML: {}". From the console dump of req, I see that body is indeed empty, but further examination shows that it has received all the other data. Here is the header:

headers: 
{ host: 'localhost:12345',
  connection: 'keep-alive',
  'content-length': '44',
  origin: 'http://localhost:12345',
  'x-requested-with': 'XMLHttpRequest',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11',
  'content-type': 'application/xml',
  accept: 'application/xml, text/xml, */*; q=0.01',
  referer: 'http://localhost:12345/signup/',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' }

I'm new to Ajax, jQuery, and Node.js, so I don't know what I'm doing wrong. I feel like it's something little I'm missing.

Thanks.

1 Answer 1

1

Try this:

$.ajax({
    url: "submit",
    type: "POST",
    contentType: "application/xml",
    data: "<user>John Smith</user>", // remove <?xml ...
    dataType: "xml",
    success: function(data, textStatus, jqXHR) { alert(data); },
    error: function(jqXHR, textStatus, errorThrown) { alert(errorThrown); }
});
Sign up to request clarification or add additional context in comments.

8 Comments

But I set contentType: "application/xml" to indicate that the request data type is XML. Did I miss something?
Yes. The header that you set in the server-side script receiving the request denotes the return type. So, for example, if you request a server side script to serve up an image, you set the contentType to image/jpeg, for example. Does that help?
Based on your response, I don't see the difference between dataType and contentType. You have said they are both the return type of the data received from the server.
Oh, I apologize. I thought you meant the header contentType. I'll update my answer.
Hmm...the data still doesn't seem to be showing up on the server side. It would appear in req.body, right? The server can respond with XML though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.