从节点JavaScript返回到jQuery的Ajax调用落在错误函数上

时间:2020-11-30 21:18:35

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

我将jQuery作为客户端脚本,将节点JavaScript服务器作为后端。

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

我曾经将请求发布到节点服务器以获取数据。请在下面找到我的AJAX电话。

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

我的问题是,即使我的响应设置为成功,它总是出现在jquery.AJAX方法的错误函数中。

And error object looks like

而错误对象看起来像

从节点JavaScript返回到jQuery的Ajax调用落在错误函数上

$.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.

现在,在我的节点JavaScript路由下面找到上述请求。

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:

generateWebServiceResponse方法如下所示:

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.

即使在失败的情况下,它也应该在isSuccess = false的情况下成功运行。但是在每种情况下都会出现错误情况。

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

我也无法解决附图中提到的访问来源错误。

1 个解决方案

#1


2  

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

这是因为CORS。您必须为您的路线启用CORS。最简单的方法是使用Node.js CORS中间件

#1


2  

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

这是因为CORS。您必须为您的路线启用CORS。最简单的方法是使用Node.js CORS中间件