Javascript 使用JQuery&;发送包含输入和文件的表单数据;AJAX到PHP Web服务
我正在尝试使用Jquery和Ajax将表单中的所有数据发送到PHP REST Web服务 JSJavascript 使用JQuery&;发送包含输入和文件的表单数据;AJAX到PHP Web服务,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在尝试使用Jquery和Ajax将表单中的所有数据发送到PHP REST Web服务 JS $("#idform").submit(function(event) { /* Stop form from submitting normally */ event.preventDefault(); //Get Data var form = $(this), formData = form.serialize(), formUrl = form.attr('action'), form
$("#idform").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
//Get Data
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method');
/* Send the data using post and put the results in a div */
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
dataType: "json",
//processData: false, // tell jQuery not to process the data
//contentType: false // tell jQuery not to set contentType
success: function(res){
if (res.result == '0') {
$("#idform").html('<div class="text-lg">Good Job!</div>');
} else {
//$("#respuesta").html('Error:');
$("#respuesta").html(res.message);
}
},
error:function(res){
$("#respuesta").html('Error comunicating webservice');
}
});
$.ajax({
url: "web_service.php",
data:'value1=1&value2='+$("#num").html()+'&value3=G',
cache: false,
async : true,
type: "POST",
....
当我从表单直接发布到php文件时,一切都正常,但当我尝试使用ajax发送数据时,一切都不正常。PHP web服务接收所有输入,但不接收“我的两个文件”:(
对不起,英文写得很差!我想你必须像这样使用JS JS
$("#idform").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
//Get Data
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method');
/* Send the data using post and put the results in a div */
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
dataType: "json",
//processData: false, // tell jQuery not to process the data
//contentType: false // tell jQuery not to set contentType
success: function(res){
if (res.result == '0') {
$("#idform").html('<div class="text-lg">Good Job!</div>');
} else {
//$("#respuesta").html('Error:');
$("#respuesta").html(res.message);
}
},
error:function(res){
$("#respuesta").html('Error comunicating webservice');
}
});
$.ajax({
url: "web_service.php",
data:'value1=1&value2='+$("#num").html()+'&value3=G',
cache: false,
async : true,
type: "POST",
....
在doc.php中,您有连接到php中web服务的代码,并在上面的数据行中发送参数