I have a problem with $https.post parameters on AngularJS. I read that AngularJS is creating a JSON for the params. So I applied the solution found here:
AngularJS - Any way for $http.post to send request parameters instead of JSON?
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
But I still have problem with Python CGI.
[Edit]
Output of the console.log for objData:
Object {hostname: "test", id_venue: 20}
inspecting Request Payload:
action: "insert",
objData: { first: "test", second:"test2" }
[/Edit]
Website call
angular.module('myApp.services', [])
.service('MyService', ['$http', '$q', function ($http, $q) {
this.insert = function (objData){
console.log(objData);
var deferred = $q.defer();
$http.post('api/api.py', { action: 'insert', objData: objData }).success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (response) {
deferred.reject("error");
});
return deferred.promise;
};
}])
Server side code is made with Python and CGI. It is unfortunately a constraint that I cannot use web frameworks.
api/api.py
import cgi
params = cgi.FieldStorage()
print 'Content-type: application/json'
print
print params
print params['objData'].value
Inspective the request headers and response, and I have obviously a KeyError:
FieldStorage(None, None, [MiniFieldStorage('action', 'insert'), MiniFieldStorage('objData[first]', 'test'), MiniFieldStorage('objData[second]', 'test2')])
KeyError: 'objData'
Any solution on how to correctly read params on Python CGI FieldStorage. Or any way to send them correctly with AngularJS? With $http.get I don't have any problem.
Maybe one solution could be to handle POST request directly in Python without using cgi.FieldStorage.