如何纠正内容配置错误

时间:2018-06-29 15:08:23

标签: javascript jquery ajax

始终感谢您的支持。我目前正在尝试通过ajax / jquery上传媒体。我可以上传图像,但是每当我尝试上传视频时,我以前都没有提供内容配置。所以我添加了这个:

xhr.setRequestHeader("content-Disposition", "attachment; filename=small.mp4");

我现在得到的是标题预检问题:

无法加载http://myurl/wp-json/wp/v2/media:在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段content-Disposition。

这是我的wordpress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js|mp4)$">
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Content-Disposition, Accept"

</FilesMatch>
</IfModule>

# END WordPress

我的请求代码如下:

$.ajax({
        url: 'http://myurl/wp-json/wp/v2/media',
        method: 'POST',
        data: formData,
        crossDomain: true,
        contentType: 'application/json',
        contentType: false,

        contentDisposition: 'form-data',//'attachment, filename="small.mp4"',
        //contentDisposition: 'attachment, filename="small.mp4"'
        processData: false,

        beforeSend: function (xhr) {
            xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode( 'admin:Rastrastvibe' ) );
            xhr.setRequestHeader("content-Disposition", "attachment; filename=small.mp4");

        },
        success: function( data ) {
            console.log( data );
            var featuredimageid = data.id;
            formData.append("featured_media", featuredimageid);


            $.ajax({
                url: 'http://myurl/wp-json/wp/v2/posts',
                method: 'POST',
                data: formData,
                crossDomain: true,
                contentType: 'application/json',
                contentType: false,
                processData: false,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode( 'admin:pass' ) );
                },
                success: function( data ) {

                    console.log( data );
           // var featuredimage= data.id;
           // console.log(featuredimage);



       },
       error: function( error ) {
        console.log( error );

    }
});


        },
        error: function( error ) {
            console.log( error );


        }
    });
});

1 个答案:

答案 0 :(得分:0)

感谢社区,我对此进行了整理。首先,我按如下方式重新编辑我的wordpress .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Content-Disposition, Accept"
</IfModule>


# END WordPress

然后,我使用表单提交文件,而不是通过formdata对象cos提供文件位置,我抵制了发送文件的表单的文件输入。现在,我要添加特征图片和视频附件的下一个挑战。