这可能是一个愚蠢的问题,但我有一个晚上.在我正在开发RESTful API的应用程序中,我们希望客户端以JSON格式发送数据.此应用程序的一部分要求客户端上载文件(通常是图像)以及有关图像的信息.
我很难跟踪单个请求中如何发生这种情况.是否可以将文件数据Base64转换为JSON字符串?我是否需要向服务器发送2个帖子?我不应该为此使用JSON吗?
作为旁注,我们在后端使用Grails,这些服务由本机移动客户端(iPhone,Android等)访问,如果其中任何一个有所不同.
我已经尝试过 console.log并使用它循环for in.
这是FormData上的MDN参考.
两次尝试都在这个小提琴中.
var fd = new FormData(),
key;
// poulate with dummy data
fd.append("key1", "alskdjflasj");
fd.append("key2", "alskdjflasj");
// does not do anything useful
console.log(fd);
// does not do anything useful
for(key in fd) {
console.log(key);
}
Run Code Online (Sandbox Code Playgroud)
如何检查表单数据以查看已设置的键.
我有一个带有remote = true的简单表单.
此表单实际上位于HTML对话框上,一旦单击"提交"按钮,该对话框就会关闭.
现在,我需要在表单成功提交后对主HTML页面进行一些更改.
我尝试使用jQuery.但这并不能确保在表单提交的某种形式的响应之后执行任务.
$("#myform").submit(function(event) {
// do the task here ..
});
Run Code Online (Sandbox Code Playgroud)
如何附加回调,以便我的代码只有在成功提交表单后才能执行?有没有办法在表单中添加一些.success或.complete回调?
我正在使用此脚本上传我的图像文件:http://jsfiddle.net/eHmSr/
$('.uploader input:file').on('change', function() {
$this = $(this);
$('.alert').remove();
$.each($this[0].files, function(key, file) {
$('.files').append('<li>' + file.name + '</li>');
data = new FormData();
data.append(file.name, file);
$.ajax({
url: $('.uploader').attr('action'),
type: 'POST',
dataType: 'json',
data: data
});
});
});
Run Code Online (Sandbox Code Playgroud)
但是当我点击上传按钮时,JavaScript控制台会返回以下错误:
Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?
我有一个看起来像这样的表格
<form action="receiver.pl" method="post">
<input name="signed" type="checkbox">
<input value="Save" type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
当我点击提交但仍然receiver.pl执行时,我想留在同一页面上.
应该怎么做?
我尝试为Symfony 2上传ajax脚本.Chrome返回此错误:
未捕获的TypeError:非法调用jquery.min.js:4
我认为这是由于FormData对象没有正确构造(我尝试使用以下脚本.serialized():
$(document).ready(function() {
$('#formImage').submit(function(event) {
event.preventDefault();
// appel Ajax
alert("ajax");
var input = document.getElementById("rasta_blogbundle_imagetype_file");
console.log(input);
var formdata = false;
if (window.FormData) {
formdata = new FormData();
console.log('formdata initialized ...');
}
else{
console.log('formdata not supported');
}
formdata.append('name',$('#rasta_blogbundle_imagetype_name').val());
console.log(formdata);
formdata.append('file',input);
formdata.append('_token',$('#rasta_blogbundle_imagetype__token').val());
console.log(formdata);
//alert(DATA);
if (formdata){
$.ajax({
url: $(this).attr('action'), // le nom du fichier indiqué dans le formulaire
type: $(this).attr('method'), // la méthode indiquée dans le formulaire (get ou post)
cache: false,
//data : $(this).serialize(),
data: …Run Code Online (Sandbox Code Playgroud) 我目前通过AJAX使用以下代码发布我的表单:
$(document).ready(function(){
$("form#createForm").submit(function() { // loginForm is submitted
$("form#createForm input#createForm_submit").attr('disabled','disabled');
tinyMCE.triggerSave();
$.ajax({
type: "POST",
dataType: "json",
url: "perform", // URL of the Perl script
data: $("#createForm").serialize(),
// script call was successful
// data contains the JSON values returned by the Perl script
success: function(data){
$('div.form-group').each(function(){
$(this).removeClass('has-error');
});
if (data.error) { // script returned error
var myList = $('ul.msg-list').empty();
$.each(data.msg, function(key,item) {
$("div."+key).addClass('has-error');
$('<li>').text(item.errtxt).appendTo(myList);
});
$('div#create_createresult').html('some error').html(myList);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
} // if
else
{ // login was successful
//$('form#login_loginform').hide();
$('div#create_createresult').text(data.msg); …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过ajax上传文件以及表单中的某些字段.但是,它不起作用.我收到这个错误.
未定义的索引: - 文件
这是我的代码.
HTML
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="file">Upload Software / File</label>
<div class="col-md-4">
<input id="file" name="file" class="input-file" type="file">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="price">Price($)</label>
<div class="col-md-4">
<input id="price" name="price" type="text" placeholder="Price" class="form-control input-md" required="">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
阿贾克斯
$("#add_product").click(function(e){
e.preventDefault();
product_name = $("product_name").val();
//d = $("#add_new_product").serialize();
$.ajax({
type: 'POST',
url: 'ajax.php',
data: $("#add_new_product").serialize(),
success: function(response)
{
//
alert(response);
}
})
});
Run Code Online (Sandbox Code Playgroud)
PHP
if (0 < $_FILES['file']['error'])
{
echo ":!";
}
else …Run Code Online (Sandbox Code Playgroud) 我正在使用JQuery发布表单数据,我已将其添加到我的函数中以允许它发布/上传文件:
mimeType:"multipart/form-data",
Run Code Online (Sandbox Code Playgroud)
我在这里用我的HTML表单调用它:
<form id="form1" method="post" action="/tickets/record?type=<?php echo $_GET["type"]; ?>&seq=<?php echo $_GET["seq"]; ?>" enctype="multipart/form-data" onsubmit="post_form('#form1');">
Run Code Online (Sandbox Code Playgroud)
并尝试使用以下方法处理PHP中的附件:
$attachment_array = array();
foreach($_FILES['ticket_update_files']['name'] as $key => $value) {
if(!$_FILES['ticket_update_files']['error'][$key]) {
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有认识到任何已被选中的文件.
我的完整jquery函数是:
function post_form(form_id, type, redir_url, loading_modal) {
type = type || '';
redir_url = redir_url || '';
loading_modal = loading_modal || '';
$( form_id ).submit(function(e) {
var formObj = $(this);
var formURL = formObj.attr("action");
var formData = new FormData(this);
CheckRequired(e);
if(loading_modal === '1') { } else {
LoadModalBody('<h2 align="center">Loading...</h3><p align="center"><i …Run Code Online (Sandbox Code Playgroud)