简体   繁体   English

angular.js:9827 POST http:// localhost:64340 / Content 405(不允许使用方法)

[英]angular.js:9827 POST http://localhost:64340/Content 405 (Method Not Allowed)

Can any one help me out with this.... I am trying to browse file and want to save into some folder using angular. 任何人都可以帮我这个忙吗?...我试图浏览文件,并希望使用angular保存到某个文件夹中。 Here is my HTML code 这是我的HTML代码

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myApp">

    <div ng-controller="myCtrl">
        <input type="file" file-model="myFile" />
        <button ng-click="uploadFile()">upload me</button>
    </div>
    <script src="app/fileUpload.js"></script>  
</body>
</html>

My JS Controller File: 我的JS控制器文件:

var myApp = angular.module('myApp', []);

myApp.config([
        '$httpProvider',
        function($httpProvider) {
            $httpProvider.defaults.withCredentials = true;
        }
]);

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function () {
                scope.$apply(function () {
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function (file, uploadUrl) {
        var fd = new FormData();
        fd.append('file', file);

        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: { 'Content-Type': undefined }
        })

        .success(function () {
        })

        .error(function () {
        });
    }
}]);

myApp.controller('myCtrl', ['$scope', 'fileUpload', function ($scope, fileUpload) {
    $scope.uploadFile = function () {
        var file = $scope.myFile;

        console.log('file is ');
        console.dir(file);

        var uploadUrl = "/Content";

        fileUpload.uploadFileToUrl(file, uploadUrl);
    };
}]);

My Web.config File : I have added 'Access-Control-Allow-Origin' but still it is not working. 我的Web.config文件:我添加了“ Access-Control-Allow-Origin”,但仍然无法正常工作。 Is there anything I am misssing ? 我有什么想念的吗?

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>``

Error Pic : 错误图片:

无法上传文件

405 errors often arise with the POST method. POST方法通常会出现405错误。 You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form. 您可能正在尝试在Web站点上引入某种输入形式,但是并非所有ISP都允许处理该形式所必需的POST方法。

the server needs you to specify a Content-Type and/or Accept in your request header. 服务器需要您在请求标头中指定Content-Type和/或Accept

$http.post(uploadUrl, fd, {
    transformRequest: angular.identity,
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
})

In the server side add this header to the response: 在服务器端,将此header添加到响应中:

Access-Control-Allow-Methods:POST, GET, PUT, DELETE, OPTIONS

If your server using PHP I use this script: 如果您的服务器使用PHP,则使用以下脚本:

    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With, X-YOUR-CUSTOM-HEADER');
    header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM