0

I have jQuery as client script and node JavaScript server as back-end.

I used to post the request to node server for getting data. Please find my AJAX call below.

My problem is even if my response is set to success it always comes in error function of the jquery.AJAX method.

And error object looks like

enter image description here

$.ajax({
        url : 'http://127.0.0.1:3000/uploadcontent/fileUpload',
        type : 'post',
        data : formData,
        enctype : 'multipart/form-data',
        processData : false,
        contentType : false,
        error : function(error,jqXHR, exception) {
            errorMessage(error);
            console.log(JSON.stringify(error))
            console.log(exception +"  :  "+error);
        },
        success : function(data) {
            alert(data);
            if (data.sucess){
                successMessage(data.returnMessage);
            } else {
                alert('error');
                if (data.returnObj != null) {

                } else{
                    errorMessage(data.returnMessage);
                }
            }
        }
    })

Now, find below my node JavaScript route for the above request.

app.post('/fileupload', function(req, res) {

    var fstream;
    var fileName, bunchId, standard, userKey, subject, unit, chapter, topic;
    req.pipe(req.busboy,function(err) {
        if(err) {
            console.log("ERROR: " + err.code + " (" + err.message + ")");
            return;
        }
    });
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename);          
        fstream = fs.createWriteStream(config.content_upload_path.pdf_path + filename + 'abc');         
        file.pipe(fstream,function(err) {
            if(err) {
                console.log('pipe error due to stream')
                console.log("ERROR: " + err.code + " (" + err.message + ")");
                return;
            }
        });
        fstream.on('close', function () {
            fileName = filename;
            fileObject = file;       
            uploadFile(paramData, res)
            res.redirect('back');
            console.log('method complete')
        });

        fstream.on('error', function (err) {
            console.log('stream on error event')
            console.log(err)
            generateWebServiceResponse(res, false, null, 200, err)
            console.log(res);
            res.send()              
        });

    });}

generateWebServiceResponse method looks like this:

function generateWebServiceResponse(response, isSuccess, result, httpStatus,
    returnMessage) {
response.json({
    isSucess : isSuccess,
    returnObj : result,
    returnMessage : returnMessage,
    resultCode : 0,
    httpStatus : httpStatus
});}

Even in case of failure it should land in success function with isSuccess = false. But in each case it is landed in error case.

I am also not able to solve access origin error mentioned in the attached image.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ankit Patel
  • 142
  • 1
  • 13

1 Answers1

2

It happened because of CORS. You must enable CORS for your route. The simplest way is using Node.js CORS middleware

Cuong Vu
  • 3,423
  • 14
  • 16
  • I have used cors in app.js .. like var cors = require('cors'); app.use(cors()); And I get rid of that access origin error ... thanks .... Now, another problem which I defined is : return object is : object : Object – Ankit Patel Jun 11 '18 at 11:29
  • Ok .. I got the object which I wanted ! .... But.... in the ajax.done() event ... not in ajax.success() event ... What can be the reason for that ? – Ankit Patel Jun 11 '18 at 11:42