使用带有jQuery的Wp Rest API上传图像

时间:2018-08-18 14:30:41

标签: wordpress rest api

我正在尝试使用来自本地主机的jquery将图像上传到wordpress媒体库。这是我的代码

function get_image_id(filename) {
        var img = new Image();
        img.src = filename; //eg: dog.jpg (would be in the same directory as the html file)
        $.ajax
            ({
                type: "POST",
                url: "https://example/wp-json/wp/v2/media",
                beforeSend: function (request) {
                    request.setRequestHeader("Authorization", "Basic xxxxxxxxxxxxx");
                    request.setRequestHeader("Content-Disposition", `attachment; filename=${filename}`);
                },
                async: false,
                data: img,
                cache: false,
                contentType: 'json',
                processData: false,
                success: function (data) {
                    result = data;
                    console.log(data)
                }
            }).fail(function(data){
                console.log(data)
            });
    }

这是我在控制台中遇到的错误:

Failed to load https://example.com/wp-json/wp/v2/media: Request header field Content-Disposition is not allowed by Access-Control-Allow-Headers in preflight response.

我该如何绕过呢?

0 个答案:

没有答案
相关问题