Gustavo Rojo Blasquez Gustavo Rojo Blasquez - 1 year ago 102
HTTP Question

In AngualrJS using $http, how to get the value of a variable set in Python CGI?

I'm making a POST request from AngularJS to Python.
I started with an JavaScript example. It works properly returning all the values.
However, when I try to do it from AngularJS I'm not able to read the value of the variable posted.

JAVASCRIP EXAMPLE THAT WORKS PROPERLY (I'm able to get the value (Mike) back of Name):
JS code

<script language="Javascript">
function asyncChange()
var request;
if (window.XMLHttpRequest) {
request = new window.XMLHttpRequest();
} else {
// Versiones antiguas de Internet Explorer.
request = new window.ActiveXObject("Microsoft.XMLHTTP");
}"POST","" , true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function()
if (request.readyState == 4 && request.status == 200)
document.getElementById("myLabel").innerHTML = "Hello " + request.responseText + "!";

import cgi
input = cgi.FieldStorage()
print "Content-type: text/html\n\n"
print "input[Pe].value: "
print input["Pe"].value

ANGULARJS DOESN'T WORK PROPERLY (I'm not able to get the value (Mike) back of Name):
Angularjs code:

'use strict'
var sectest= {


function sectestCtrl($http){
var prac= this;
prac.method = 'POST';
prac.url = ''; = {Name : 'Mike'};
prac.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };

prac.sendHTML = send;

function send(){
prac.code = null;
prac.response = null;
$http({method: prac.method, headers: prac.headers, url: prac.url, data: $.param(}).
then(function(response) {
prac.status = response.status;
prac.data_answer =;
console.log("OK prac.data_answer: ", prac.data_answer)
}, function(response) {
prac.data_answer = || 'Request failed';
prac.status = response.status;

})(); code


import json
import cgi
input = cgi.FieldStorage()

print "Content-type: text/html\n\n"
print input["Name"].value

The problem is that prac.data_answer prints blank value.
I have already try with different headers for both angularjs and python codes but none seems to work:

prac.headers = { 'Content-Type': 'application/json' };
prac.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
prac.headers = { 'Content-Type': 'text/html\n\n' };

Many thanks.

Answer Source

There are 2 separate issues you're trying to address. Server (CGI) & client(angularjs). First check to see that you are receiving the data over the network - using Chrome developer tools, under the Network tab. If so, there's no need to change the Content-Type to json, since angular by default assumes all http data is in json format.

I don't think you need all those attributes for a post request. Seems like an overkiller when it can be simpler. Try this:

$, data).then(function(response){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download