john john - 1 month ago 15
AngularJS Question

How to send POST with multiple params in angularjs?

I want to send multiple parameters using angularjs HTTP post service.
This is my function to send post using in my project

function getListObject(table,language) {
var url=API_URL+table+"?";
var companyTemp={company_id:40743};
var languageTemp={language_id:language}
var cm = "list";
var dtJSON = JSON.stringify({table: JSON.parse(JSON.stringify()),company:JSON.parse(JSON.stringify(companyTemp)),languages: JSON.parse(JSON.stringify(languageTemp))});
var data = $.param({
cm: cm,
dt: dtJSON
});
return $http.post(url+data).then(handleSuccess, handleError('Error get list'));}


Example my url is: localhost:8080/api/product?cm=list&dt={product:{},company:{company_id:40743},languages:{language_id:"en"}}

When I use above function, I write: Service.getListObject("products","en").
I have a problem in this line:

var dtJSON = JSON.stringify({table: JSON.parse(JSON.stringify(companyTemp)),languages: JSON.parse(JSON.stringify(languageTemp))});


I want result dtJSON this line is:

"{"products":{},"company":{"company_id":40743},"languages":{"language_id":"en"}}"


But I receive this :

"{"table":{},"company":{"company_id":40743},"languages":{"language_id":"en"}}"


How to fix this?

Answer

Documentation : $http

$http.post(url, data, config);

$http will handle itself the JSON parsing thus you do not need to do it yourself

function getListObject(table, language) {
    var url = API_URL;
    return $http.post(url , {company_id : 40743, language_id : language, cm : "list"});
}
Comments