我正在使用Blueimp JQuery文件上传,选项forceIframeTransport:true,我可以将文件和表单字段上传到跨域端点进行处理。
但是,服务器响应很长时间都会让我烦恼。
我遇到过关于图书馆的在线资料:
http://missioncriticallabs.com/blog/lessons-learned-from-jquery-file-upload
我已按照链接的说明操作,但无法正确获取服务器响应。
这是我的代码:
jQuery("#newFile").fileupload({
url: "https://mydomain/cgi-bin/get_file.cgi",
forceIframeTransport: true,
type: "POST",
cache: false,
contentType: false,
processData: false,
autoUpload: false,
replaceFileInput: false
done: function(e, data) {
console.log("done");
console.log(e);
console.log(data.result);
}
});
目前,我的服务器端点仅打印JSON响应:
#!C:/Perl64/bin/perl.exe
use CGI;
use JSON;
print "Content-type: text/plain; charset=iso-8859-1\n\n";
#print header("application/json;charset=UTF-8");
my %rawText = (message => "Processed");
my $json = encode_json \%rawText;
print $json;
我尝试将标题打印为“text / plain”和“application / json”,但我无法在JS代码中的“完成”函数中获取服务器响应。
我的console.log(data.result)始终打印为“未定义”。
我对图书馆和背后的概念很新鲜,
所以如果有人能引导我使用该库,我将不胜感激。
非常感谢。
答案 0 :(得分:1)
为了使小部件与Perl一起使用,您必须implement a custom server-side upload handler。您的CGI脚本必须以文档中描述的格式返回JSON响应。
幸运的是,有人已经为你完成了这项工作。请查看CPAN上的模块jQuery::File::Upload。