我在 html 中有这个表单,其中包含文件和其他输入元素:
<form action="/imsick/disease/add" id="addDisease" method="post" role="form" data-async="" enctype="multipart/form-data">
<input type="text" id="name" name="name" class="form-control">
<textarea id="description" name="description" class="form-control" rows="10"></textarea>
<input type="checkbox" name="category[]" value="3">
<input type="checkbox" name="category[]" value="5">
<input type="checkbox" name="category[]" value="6">
<input type="checkbox" name="category[]" value="4">
<input type="checkbox" name="category[]" value="1">
<input type="checkbox" name="category[]" value="7">
<input type="checkbox" name="category[]" value="2">
<input type="file" id="approach-file" name="approach">
<input type="checkbox" name="symptom[0][is_patogonomic]">
<input type="text" name="symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">
<input type="checkbox" name="symptom[1][is_patogonomic]">
<input type="text" name="symptom[1][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">
<input type="text" name="lab_symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/labsymptom/search">
<select name="lab_symptom[0][anomaly_type]" class="form-control">
<option value="ABOVE">ABOVE</option>
<option value="UNDER">UNDER</option>
<option value="POSITIVE">POSITIVE</option>
<option value="NEGATIVE">NEGATIVE</option>
<option value="++">++</option>
</select>
<input type="submit" class="btn btn-default center-block">
</form>
这是我通过 ajax 提交表单数据的 javascript/jquery 代码:
$('form[data-async]').on('submit', '', function(event) {
//form data object
event.preventDefault();
var $form = $(this);
var __fd = new FormData($form[0]);
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: __fd,
processData: false,
beforeSend: function(xhr) {
//not important
},
success: function(data, status) {
data = $.parseJSON(data);
if (data.success) {
//not important
} else {
//not important
}
},
error: function() {
//not important
}
});
return false;
});
提交数据后,在表单的目标 URL 上执行 var_dump($_POST),将得到以下内容:
array(2) {
["_url"]=>
string(12) "/disease/add"
["------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition:_form-data;_name"]=>
string(1404) ""name"
asdasdaasdasdasdasda
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="description"
asdsad
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
5
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
4
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
1
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
7
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="approach"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[0][is_patogonomic]"
on
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[0][name]"
asdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[1][is_patogonomic]"
on
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[1][name]"
asdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="lab_symptom[0][name]"
asdasdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="lab_symptom[0][anomaly_type]"
ABOVE
------WebKitFormBoundaryHmT1pwGJDYbVxvrN--
"
}
似乎 php 不解析此数据并且 $_POST 数组只有两个成员。 谁能告诉我我做错了什么?
谢谢
最佳答案
尝试添加contentType: false
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: __fd,
processData: false,
contentType: false , // tell jQuery not to set contentType
beforeSend: function(xhr) {
//not important
},
success: function(data, status) {
data = $.parseJSON(data);
if (data.success) {
//not important
} else {
//not important
}
},
error: function() {
//not important
}
});
关于javascript - php 不解析来自 ajax 发送的 multipart/form-data 表单数据的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839009/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
“输出”是一个序列化的OpenStruct。定义标题try(:output).try(:data).try(:title)结束什么会更好?:) 最佳答案 或者只是这样:deftitleoutput.data.titlerescuenilend 关于ruby-on-rails-更好的替代方法try(:output).try(:data).try(:name)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_