2016-03-01 29 views
0

我試圖用$資源向API服務器發出請求。 我想做一個帖子,但角度變成post選項的方法,並給出一個錯誤,如 選項http://l ocalhost/API.DWS/api/v1 /用戶/登錄 XMLHttpRequest無法加載http://localhost/API .DWS/API/V1 /用戶/登錄。預檢響應具有無效的HTTP狀態碼405角度資源POST方法變成選項

var objectMethods = { 
 
    get: { method: 'GET' }, 
 
    update: { method: 'PUT' }, 
 
    create: { method: 'POST' }, 
 
    remove: { method: 'DELETE' }, 
 
    patch: { method: 'PATCH' } 
 
}; 
 

 

 

 
var apiUrl = "http://localhost/API.DWS"; 
 

 
angular.module('nurby.version.services', []) 
 
    .config(function ($httpProvider) { 
 
    }) 
 

 

 

 
.factory('LoginService', ['$resource', '$http', function ($resource, $http) { 
 
    return $resource(apiUrl + "/api/v1/user/login", {},objectMethods); 
 
}]) 
 
.controller('LogInController', ['$scope', '$rootScope', '$location','LoginService', '$http', function ($scope, $rootScope, $location, LoginService, $http) { 
 
    $scope.login = function (model) { 
 
     var loginObject = { Username: model.username, Password: model.password }; 
 
     
 
     
 
     $http.defaults.useXDomain = true; 
 
     $http.defaults.headers['Content-Type'] = 'application/json'; 
 
     $http.defaults.headers['Access-Control-Allow-Origin'] = '*'; 
 

 
     LoginService.create({}, loginObject, function (data) { 
 
      if (data) { 
 
       toastr.success("itworks"); 
 
      } 
 
      else { 
 
       toastr.error("not working") 
 
      } 
 
     }) 
 

 
    } 
 

 
}]);

+0

份額一些代碼,所以我可以告訴你,正是你做錯 –

+0

ok.here是我的代碼。 –

+0

你用JSONP的方法嘗試嗎? – jithin

回答

0

可以定義service.js並使用它象下面這樣:

var APP_NAME = 'app'; 

angular.module(APP_NAME).service('WebService', ["$http", function ($http) { 
this.login = function (parameters,callbackFunc) 
{ 
    $http({ 
     url: 'api/login', 
     method: 'POST', 
     headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
     data: $.param(parameters) 
    }).success(function (data) { 
     callbackFunc(data); 
    }).error(function (data) { 
     callbackFunc([]); 
    }); 
}; 

,並用它在你的控制器象下面這樣:

LoginController = ['$scope', '$http', '$location', 'WebService','$window', function ($scope, $http, $location,$WebService,$window) { 

$scope.login = function(admin){ 

    var data = {email:admin.email,password:admin.password}; 
    $WebService.login(data,function(result){ 
     if(result.success){ 
      $window.location.replace("index"); 
     } 
     else{ 
      $scope.loginError = result.fail; 
     } 
    }); 
} 

}];

+0

它的工作原理,但我想使用$資源不$ $ http。其他想法? –

+0

我想你可以在這裏找到你的答案: http://stackoverflow.com/questions/13269882/angularjs-resource-restful-example –

+0

我的代碼工作。有iis配置引起的問題。非常感謝你的想法 –

0

這裏的問題是,你指定一個完整的URL開始「http://localhost/API.DWS」,你還沒有從同一個域加載網頁(也許你使用了不同的端口?)。

這意味着瀏覽器會將您的請求視爲跨域請求。因此它首先發送一個OPTIONS請求,詢問服務器是否允許你發送POST。您可以配置您的服務器以正確響應這些請求,或者更改您的代碼,以便網頁和api位於同一個域中。

如何配置您的服務器將取決於您正在運行的服務器。搜索CORS和你的網絡服務器,你應該找到有用的信息。

0

我的控制器內這個工作對我來說

var resource = $resource(
       "your_api_url", 
       { 
        callback: "JSON_CALLBACK" 
       }, 
       { 
        getData: { 
         method: "JSONP", 
         isArray: false 
        } 
       } 
      ); 

    function loadRemoteData() { 

       $scope.isLoading = true; 

       resource.getData().$promise.then(
        function(friends) { 

         $scope.isLoading = false; 


        }, 
        function(error) { 

         // If something goes wrong with a JSONP request in AngularJS, 
         // the status code is always reported as a "0". As such, it's 
         // a bit of black-box, programmatically speaking. 
         alert("Something went wrong!"); 

        } 
       ); 

      } 
    $scope.searchResources = function() { 
$scope.isLoading = true; 

       resource.getData().$promise.then(
        function(friends) { 

         $scope.isLoading = false; 

        }, 
        function(error) { 

         // If something goes wrong with a JSONP request in AngularJS, 
         // the status code is always reported as a "0". As such, it's 
         // a bit of black-box, programmatically speaking. 
         alert("Something went wrong!"); 

        } 
       ); 


    };