0

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

1

1 Answer 1

1

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 () {});
Sign up to request clarification or add additional context in comments.

1 Comment

This code works fine for me, thank you I really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.