如何在jQuery中使用Ajax请求发送FormData对象?(How to send FormData objects with Ajax-requests in jQuery? [duplicate])
这个问题已经在这里有一个答案:
- 用jQuery.ajax发送multipart / formdata 10个答案
XMLHttpRequest Level 2标准(仍然是工作草案)定义了
FormData
接口。 此接口可将File
对象附加到XHR请求(Ajax请求)。Btw,这是一个新功能 - 在过去,“隐藏的iframe-trick”被使用(在我的另一个问题中阅读 )。
这是它的工作原理(示例):
var xhr = new XMLHttpRequest(), fd = new FormData(); fd.append( 'file', input.files[0] ); xhr.open( 'POST', 'http://example.com/script.php', true ); xhr.onreadystatechange = handler; xhr.send( fd );
其中
input
是一个<input type="file">
字段,handler
是Ajax请求的成功处理程序。这在所有的浏览器(除IE之外,再次)都非常出色。
现在,我想使这个功能与jQuery。 我试过这个:
var fd = new FormData(); fd.append( 'file', input.files[0] ); $.post( 'http://example.com/script.php', fd, handler );
不幸的是,这不会工作(抛出“非法调用”错误 - 屏幕截图在这里 )。 我假设jQuery期望一个表示form-field-names / values的简单键值对象,并且我传入的
FormData
实例显然是不兼容的。现在,由于可以将
FormData
实例传递到xhr.send()
,我希望它也可以使其与jQuery一起工作。
更新:
我在jQuery的Bug Tracker创建了一个“功能票”。 在这里: http : //bugs.jquery.com/ticket/9995
我被建议使用“Ajax preilter”...
更新:
首先让我演示一下演示我想要实现的行为。
HTML:
<form> <input type="file" id="file" name="file"> <input type="submit"> </form>
JavaScript的:
$( 'form' ).submit(function ( e ) { var data, xhr; data = new FormData(); data.append( 'file', $( '#file' )[0].files[0] ); xhr = new XMLHttpRequest(); xhr.open( 'POST', 'http://hacheck.tel.fer.hr/xml.pl', true ); xhr.onreadystatechange = function ( response ) {}; xhr.send( data ); e.preventDefault(); });
上述代码导致此HTTP请求:
这就是我需要的 - 我想要这个“multipart / form-data”的内容类型!
拟议的解决方案如下:
$( 'form' ).submit(function ( e ) { var data; data = new FormData(); data.append( 'file', $( '#file' )[0].files[0] ); $.ajax({ url: 'http://hacheck.tel.fer.hr/xml.pl', data: data, processData: false, type: 'POST', success: function ( data ) { alert( data ); } }); e.preventDefault(); });
但是,这导致:
您可以看到,内容类型错误...
This question already has an answer here:
- Sending multipart/formdata with jQuery.ajax 12 answers
The XMLHttpRequest Level 2 standard (still a working draft) defines the
FormData
interface. This interface enables appendingFile
objects to XHR-requests (Ajax-requests).Btw, this is a new feature - in the past, the "hidden-iframe-trick" was used (read about that in my other question).
This is how it works (example):
var xhr = new XMLHttpRequest(), fd = new FormData(); fd.append( 'file', input.files[0] ); xhr.open( 'POST', 'http://example.com/script.php', true ); xhr.onreadystatechange = handler; xhr.send( fd );
where
input
is a<input type="file">
field, andhandler
is the success-handler for the Ajax-request.This works beautifully in all browsers (again, except IE).
Now, I would like to make this functionality work with jQuery. I tried this:
var fd = new FormData(); fd.append( 'file', input.files[0] ); $.post( 'http://example.com/script.php', fd, handler );
Unfortunately, that won't work (an "Illegal invocation" error is thrown - screenshot is here). I assume jQuery expects a simple key-value object representing form-field-names / values, and the
FormData
instance that I'm passing in is apparently incompatible.Now, since it is possible to pass a
FormData
instance intoxhr.send()
, I hope that it is also possible to make it work with jQuery.
Update:
I've created a "feature ticket" over at jQuery's Bug Tracker. It's here: http://bugs.jquery.com/ticket/9995
I was suggested to use an "Ajax prefilter"...
Update:
First, let me give a demo demonstrating what behavior I would like to achieve.
HTML:
<form> <input type="file" id="file" name="file"> <input type="submit"> </form>
JavaScript:
$( 'form' ).submit(function ( e ) { var data, xhr; data = new FormData(); data.append( 'file', $( '#file' )[0].files[0] ); xhr = new XMLHttpRequest(); xhr.open( 'POST', 'http://hacheck.tel.fer.hr/xml.pl', true ); xhr.onreadystatechange = function ( response ) {}; xhr.send( data ); e.preventDefault(); });
The above code results in this HTTP-request:
This is what I need - I want that "multipart/form-data" content-type!
The proposed solution would be like so:
$( 'form' ).submit(function ( e ) { var data; data = new FormData(); data.append( 'file', $( '#file' )[0].files[0] ); $.ajax({ url: 'http://hacheck.tel.fer.hr/xml.pl', data: data, processData: false, type: 'POST', success: function ( data ) { alert( data ); } }); e.preventDefault(); });
However, this results in:
As you can see, the content type is wrong...
原文:https://stackoverflow.com/questions/6974684
最满意答案
您可能在该服务器中运行了多个cron进程。 这通常不会发生。 但无论如何确认它
ps aux | grep cron
你可以阻止它,
/etc/init.d/cron stop
要么
service cron stop
或使用'杀死PID'(不推荐)。
并开始使用start - 而不是停在上面两个命令中的任何一个。
You may have more than one cron process running in that server. This normally wont happen. But anyway confirm it with
ps aux | grep cron
You can stop it by,
/etc/init.d/cron stop
or
service cron stop
or use 'kill PID' ( not recommended ).
And to start use start - instead of stop in either of above two commands.
相关问答
更多-
使用crontab运行Makefile(Run Makefile with crontab)[2023-10-08]
crontab命令仅在最小环境变量下执行,即 PATH=/usr/bin:/bin (无论如何都是debian), 因此,如果您依赖于$PATH ,它将失败。 考虑指定渗透程序的绝对路径,无论它在何处被调用。 或者,您可以在脚本中更改$PATH本身 export PATH="/my/bin:$PATH" ps:您可以通过添加简单的cron作业来检查环境 * * * * * env > /tmp/env.txt crontab commands are executed only with minimal ... -
您可能在该服务器中运行了多个cron进程。 这通常不会发生。 但无论如何确认它 ps aux | grep cron 你可以阻止它, /etc/init.d/cron stop 要么 service cron stop 或使用'杀死PID'(不推荐)。 并开始使用start - 而不是停在上面两个命令中的任何一个。 You may have more than one cron process running in that server. This normally wont happen. Bu ...
-
crontab没有运行php脚本(crontab not running php script)[2022-04-21]
语法* * * * * /var/www/html/testsession.php > /var/www/html/log有效。 很可能,因为它是crontab中的最后一行,它缺少换行符。 Cron在每个条目的末尾都需要换行; 换句话说,你的crontab必须以空行结束。 从man crontab的“诊断”部分: cron要求crontab中的每个条目以换行符结尾。 如果crontab中的最后一个条目缺少换行符,cron将认为crontab(至少部分)已损坏并拒绝安装它。 您可能希望将>替换为>>以便将新内 ... -
这取决于你的crontab中的内容。 如果在其中设置了任何环境变量,则应该检查并修复它们。 除此之外,你应该没问题。 你的shell(应该)已经停止尝试在第一个*执行每一行,除非扩展本身产生了一个有效的命令。 It depends on what was in your crontab. If you set any environment variables in it, you should probably check and fix them. Apart from that, you should ...
-
crontab没有运行命令(crontab not running command)[2022-03-14]
我不知道为什么不运行但是在那之前发生了同样的事情 * * * * * / home / [user] / pushDelivery 尝试这个 * / 1 * * * * / home / [user] / pushDelivery 它以这种方式为我工作......(每一分钟都有任务) I fixed the issue. In my pushDelivery file I was supposed to write: /usr/bin/flock -n /home/[user]/PushChatServer ... -
Crontab没有运行(Crontab not running)[2022-05-27]
你缺少环境变量。 尝试这样的简单测试 将此添加到您的crontab * * * * * set > /tmp/vars 等待2-3分钟,返回并删除刚刚创建的crontab条目。 下一个, 从通常使用的shell命令行 set > myvars diff myvars /tmp/vars 这将向您展示环境的不同之处。 修改您的cron作业环境。 只需添加所需内容即可。 You are missing envrionment variables. Try a simple test like this a ... -
* * * * * su esr -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 2 ...
-
假设我有一个带有内容的python文件test.py print "hello" 要安排它每30分钟运行一次,请使用 crontab -e 然后编辑添加 */30 * * * * python /path-to-file/test.py 检查cron是否成功运行 grep CRON /var/log/syslog 在这里,你会看到日志,线条等 May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py ...
-
我发现了导致我的错误的原因(其他人可能/可能不同)。 通过许多源文件调试后,我发现问题的根源是没有加载PDO扩展。 我不知道为什么只有在作为一个cron-job运行时才是这种情况。 需要注意的是:似乎其他具有相同问题的人在配置阶段经常会遇到这种情况,因此如果您获得相同的异常,从那里开始调试可能是个好主意。 I've found what was causing my error in particular (it may/may not vary for others). After debugging t ...
-
crontab没有运行wget(crontab not running wget)[2024-05-23]
在crontab -e ,将其crontab -e第一行: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 然后wget应该工作而不指定完整路径。 您也可以只指定wget的完整路径(找到它的which wget ): /usr/bin/wget --output-document="/Users/proudowner/Desktop/tfgo/bp.json" http://backpack.tf/api/IGetMark ...