简体   繁体   中英

How to pass url parameters from a controller in Angular to a php

I need to send a URL with this format to a php file.

"http://localhost/project/api.php?id="+$scope.idProd+"&price="+$scope.priceProd

Can I do it with $http.post? I have tried to do

$http.post("http://localhost/project/api.phpid="+$scope.idProd+"&price="+$scope.priceProd)      
.success(function(res){
                  console.log(res);
});

My php not received correctly this url and displays the following errors: Undefined index: id and Undefined index: price

Those are url-encoded params. You do it like this:

$http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    //function to serialize data to url-encoded data
    transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    //the data you're passing
    data: {id: $scope.idProd, price: $scope.priceProd}
}).success(function () {});

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