将.ajax转换为$ http - 方法不允许(405)

时间:2015-06-18 18:48:37

标签: ajax angularjs

我正在尝试将此.ajax转换为$ http以用于Angular应用程序,但它正在抛出一个"方法不允许"错误。我相信我错过了一些明显的东西,但无法弄清楚失败的原因。

这是.ajax版本:

function uploadFileSync(spWebUrl , library, filename, file) 
        {
            var reader = new FileReader();
            reader.onloadend = function(evt) 
            {
                if (evt.target.readyState == FileReader.DONE) 
                {
                    var buffer = evt.target.result;
                    var completeUrl = spWebUrl
                      + "/_api/web/lists/getByTitle('"+ library +"')"
                      + "/RootFolder/Files/add(url='"+ filename +"',overwrite='true')?"
                      + "@TargetLibrary='"+library+"'&@TargetFileName='"+ filename +"'";

                    $.ajax({
                        url: completeUrl,
                        type: "POST",
                        data: buffer,
                        async: false,
                        processData: false,
                        headers: {
                            "accept": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                            "content-length": buffer.byteLength
                        },
                        complete: function (data) {
                            //uploaded pic url
                            console.log(data.responseJSON.d.ServerRelativeUrl);
                            $route.reload();
                        },
                        error: function (err) {
                            alert('failed');
                        }
                    });

                }
            };
            reader.readAsArrayBuffer(file);
        } 

这是我的$ http版本:

function uploadFileSync(spWebUrl , library, filename, file) 
    {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            if (evt.target.readyState == FileReader.DONE) {
                var buffer = evt.target.result;
                var completeUrl = spWebUrl
                  + "/_api/web/lists/getByTitle('" + library + "')"
                  + "/RootFolder/Files/add(url='" + filename + "',overwrite='true')?"
                  + "@TargetLibrary='" + library + "'&@TargetFileName='" + filename + "'";

                $http({
                    url: completeUrl,
                    type: "POST",
                    data: buffer,
                    processData: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-length": buffer.byteLength
                    }
                }).success(function (data) {
                    //uploaded pic url
                    //console.log(data.responseJSON.d.ServerRelativeUrl);
                    $route.reload();
                }).error(function (err) {
                    alert(err);
                });
            }
        };
        reader.readAsArrayBuffer(file);
    }

0 个答案:

没有答案