简体   繁体   English

发送帖子时如何接收ajax响应

[英]how to receive ajax response while sending post

I have an html <input type="file" and I am sending chosen file using ajax, In my server side script i have limited file size upto 2MB and if it exceed the limit I am sending a simple message back to client so the client could understand what happend, 我有一个html <input type="file" ,我正在使用ajax发送选择的文件,在我的服务器端脚本中,我将文件大小限制为2MB,如果超出限制,我会向客户端发送一条简单的消息,以便客户端会明白发生了什么

server-side script: 服务器端脚本:

@MultipartConfig(
    maxFileSize=1024*2048     // 1Mb max
)
try{
        MultipartRequest multipartRequest = new MultipartRequest(request, "D:\\");
    } catch(IOException e){
        out.print("File limit has been exceeded");
    }

    out.print("Successfully Uploaded");

client-side send script: 客户端发送脚本:

 if(formdata){
        $.ajax({
            url: '../propicuploader',
            type: 'POST',
            data: formdata,
            processData: false,
            contentType: false,
            success: function(data){
                alert(data);
            }
        });
    }

The problem is that if the file is lower than 2MB alert box pops in client saying Successfully uploaded but if it is higher than 2MB then nothing happen although I have in my server-side script Exception to send a file limit exceed error and don't get the pop up box saying about the error in my client-side. 问题是,如果文件小于2MB,则在客户端弹出警告框,提示“已Successfully uploaded但是如果文件大于2MB,则尽管我在服务器端脚本中有异常,但file limit exceed error发送file limit exceed error并且没有得到一个弹出框,说明我的客户端中的错误。 Just guessed and I thought The problem might be ajax that server sends the error at exact time where ajax is posting the data, so can you tell me what is the appropriate way to send file limit exceeded error to client 只是猜测而已,我认为问题可能是ajax,服务器在ajax发布数据的确切时间发送错误,所以您能告诉我什么是将超出文件限制的错误发送给客户端的适当方法。

use this 用这个

 $.ajax({
            url: '../propicuploader',
            type: 'POST',
            data: formdata,
            processData: false,
            contentType: false,
            async : false,
            success: function(data){
                alert(data);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM