Eric Eric - 3 months ago 19
Javascript Question

jQuery ajax call getting error response with status of 200

I sent a jQuery ajax request to my express-powered node.js server, which processed it successfully and responded. However, the response triggers the ajax

error
callback instead of the
success
callback (despite the status code being "200"). That was not what I was expecting. ;o) Here is my code:

client.js:

$.ajax( {
url: "/httpUpload",
type: "POST",
processData: false,
contentType: false,
dataType : "json",
data: formData,
success: function() {
// Do stuff
},
error: function( error ) {
console.error( "HttpUpload failed: " + error.responseText );
}
} );


server.js:

var express = require( "express" );
var app = express();
var server = require( "http" ).Server( app );
var multipart = require('connect-multiparty');

app.use( express.static( "public" ) );
app.use( multipart() );

app.post( "/httpUpload", function( req, res ) {
console.log( "Received post request");
res.send( "Done!" );
}


The
error
object that I get in the ajax
error
callback looks like this:

{readyState: 4, responseText: "Done!", status: 200, statusText: "OK"}


Any idea why it's triggering the
error
callback?

Answer

Oh, silly me. It's the datatype that I had on the ajax call. I wasn't returning json, so it errored out. Everything worked when I removed that.