作为Salamu Alaikum,我在工作wp_ajax
并尝试通过ajax上传文件。当我processData: true
它返回错误illegal invocation
文件上载时。但当我设定processData: false
它返回错误400 bad request
. 但是,我再次检查了PHP是否一切正常。But if I do not upload file all data get saved successfully.
这是我的表格。。。
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="f-title" name="f-title" placeholder="Flyer Title" class="form-control" required="" type="text">
</div>
<div class="form-group">
<input id="f-validity" name="f-validity" placeholder="Valid Till" class="form-control" required="" type="date">
</div>
<div class="form-group">
<input id="f-ID" name="f-ID" placeholder="Flyer ID" class="form-control" required="" type="text">
</div>
<div class="form-group">
<input id="f-thumbnail" name="f-thumbnail" class="form-control" required="" type="file">
</div>
<div class="form-group">
<input id="f-company" name="f-company" class="form-control" placeholder="Company ID" required="" type="text">
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<select id="f-category" name="f-category" class="form-control" required="">
<?php
$limit = 50;
$terms = apply_filters(
\'taxonomy-images-get-terms\',
\'\',
array(
\'taxonomy\' => \'flyer-cat\',
)
);
foreach ($terms as $term) {
$term_link = get_term_link( $term );
}
?>
<?php foreach($terms as $term) { ?>
<?php $term_link = get_term_link( $term ); $limit++; ?>
<option><?php _e($term->name); ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-6">
<button class="form-control" name="f-submit" id="f-submit" type="button">Save Flyer</button>
</div>
</div>
</div>
</form>
这是我的javascript
jQuery(document).ready(function(e) {
jQuery(\'#f-submit\').on(\'click\', function(e) {
e.preventDefault();
var f_title = jQuery(\'#f-title\').val();
var f_validity = jQuery(\'#f-validity\').val();
var f_ID = jQuery(\'#f-ID\').val();
var f_company = jQuery(\'#f-company\').val();
var f_category = jQuery(\'#f-category\').val();
var f_thumbnail = jQuery(\'#f-thumbnail\').prop(\'files\')[0];
console.log(f_thumbnail);
//SaveFlyer(f_title, f_validity, f_ID, f_company, f_category, f_thumbnail);
});
function SaveFlyer(f_title, f_validity, f_ID, f_company, f_category, f_thumbnail) {
jQuery.ajax({
type: "POST",
url: \'<?php echo admin_url( \'admin-ajax.php\' ); ?>\',
cache: false,
contentType: \'json\',
dataType: false,
data: { \'action\':\'save_flyer\', \'f_title\':f_title, \'f_validity\':f_validity, \'f_ID\':f_ID, \'f_company\':f_company, \'f_category\':f_category, \'f_thumbnail\':f_thumbnail },
beforeSend: onLoading,
success: onSuccesPing,
crossDomain:true
});
}
function onLoading() {
}
function onSuccesPing(data, status) {
alert(\'Success!!\');
}
});
在我的
functions.php
, 我有。。。
function save_flyer()
{
$f_title = $_POST[\'f_title\'];
$f_validity = $_POST[\'f_validity\'];
$f_ID = $_POST[\'f_ID\'];
$f_company = $_POST[\'f_company\'];
$f_category = $_POST[\'f_category\'];
$f_thumbnail = $_POST[\'f_thumbnail\'];
$new_post = array(
\'post_title\' => $f_title,
\'tags_input\' => $f_category,
\'post_status\' => \'publish\',
\'post_type\' => \'soldy-flyer\'
);
$post_id = wp_insert_post($new_post);
add_post_meta($post_id, \'sf_flyer-id\', $f_ID);
add_post_meta($post_id, \'sf_flyer-company\', $f_company);
add_post_meta($post_id, \'sf_flyer-valid\', $f_validity);
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
$attahment_id = media_handle_upload( \'f_thumbnail\', $post_id );
set_post_thumbnail($post_id, $attahment_id);
}
add_action(\'wp_ajax_save_flyer\', \'save_flyer\');
add_action(\'wp_ajax_nopriv_save_flyer\', \'save_flyer\');
最合适的回答,由SO网友:Chetan Vaghela 整理而成
您可以使用以下代码使用ajax上载文件:
First :添加属性name="f-form"
和id="f-form"
在您的<form>
标签
之后,在js脚本中,使用new FormData()
并发送到ajax数据,
Note : 改变url: ajax_object.ajaxurl
使用ajax url路径
jQuery(document).ready(function(e) {
jQuery(\'#f-submit\').on(\'click\', function(e) {
e.preventDefault();
var form = jQuery(\'#f-form\')[0];
var varform = new FormData(form);
varform.append("action", "save_flyer");
SaveFlyer(varform);
});
function SaveFlyer(varform) {
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
dataType: "JSON",
data: varform,
processData: false,
contentType: false,
cache: false,
beforeSend: onLoading,
success: onSuccesPing,
crossDomain:true
});
}
function onLoading() {
}
function onSuccesPing(data, status) {
if(data.result == \'true\')
{
alert(\'Success!!\');
}
if(data.result == \'false\')
{
alert(\'Fail!!\');
}
}
});
最后,ajax handel请求:
function save_flyer()
{
$f_title = $_POST[\'f-title\'];
$f_validity = $_POST[\'f-validity\'];
$f_ID = $_POST[\'f-ID\'];
$f_company = $_POST[\'f-company\'];
$f_category = $_POST[\'f-category\'];
$new_post = array(
\'post_title\' => $f_title,
\'tags_input\' => $f_category,
\'post_status\' => \'publish\',
\'post_type\' => \'soldy-flyer\'
);
$post_id = wp_insert_post($new_post);
if($post_id)
{
add_post_meta($post_id, \'sf_flyer-id\', $f_ID);
add_post_meta($post_id, \'sf_flyer-company\', $f_company);
add_post_meta($post_id, \'sf_flyer-valid\', $f_validity);
if(empty($_FILES[\'f-thumbnail\'][\'error\'])){
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$attahment_id = media_handle_upload( \'f-thumbnail\', $post_id );
if(!empty($attahment_id))
{
set_post_thumbnail($post_id, $attahment_id);
}
}
$result1 = \'true\';
}
else
{
$result1= \'false\';
}
echo json_encode(array(\'result\'=>$result1));
die();
}
add_action(\'wp_ajax_save_flyer\', \'save_flyer\');
add_action(\'wp_ajax_nopriv_save_flyer\', \'save_flyer\');
如果这对你有帮助,请告诉我。