简体   繁体   中英

Ionic Rest api Put Request

Hi i am new to ionic i am trying to send the put request using https to update. In the controller I am writing like this. and I am referring https://docs.angularjs.org/api/ng/service/ $http#put

$scope.update = function(user){
        if($localStorage.hasOwnProperty("accessToken") === true) {
            var requestContent = $.param({
                facebook_url: user.facebook_url,
                twitter_username: user.twitter_username
            });
            var req = {
                method: 'PUT', 
                url: link+'user/profile?id='+$localStorage.accessToken,
                data: requestContent,
                headers: {'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Type': 'application/json'}
            }
            $http(req).success(function(data) { 
                console.log(data.message);
                console.log(data.user)  
                $ionicLoading.hide(); 
            }).error(function(data) { 
                console.log(data.message); 
                $ionicLoading.hide(); 
            });
        } else {
            console.log($localStorage.accessToken);
            console.log(data.message); 
            $ionicLoading.hide();
        }
    };

i am getting error XMLHttpRequest cannot load http://192.168.1.7/api/v1/user/profile?id=ccdec73ce10117159a6ba8bc54e4943a6e8cac81. Invalid HTTP status code 405 XMLHttpRequest cannot load http://192.168.1.7/api/v1/user/profile?id=ccdec73ce10117159a6ba8bc54e4943a6e8cac81. Invalid HTTP status code 405 with Request Method:OPTIONS

.htaccess

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

post and get is working if i had this above lines in the .htaccess but other options are not working. Thanks in advance

To me, this sounds like a CORS issue. Please see this answer that provides more detail about CORS preflight requests and Apache configuration.

Also, is there any reason you're using $http instead of $resource ?

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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