在AngualrJS中使用$ http,如何在Python CGI中获取变量集的值?

时间:2016-11-21 20:39:03

标签: python angularjs http cgi

我正在从AngularJS向Python发出POST请求。 我从一个JavaScript示例开始。它可以正常返回所有值。 但是,当我尝试从AngularJS中执行此操作时,我无法读取已发布变量的值。

JAVASCRIP示例正常工作(我能够获得Name的值(Mike)): JS代码

  <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");
      }
      request.open("POST","nctest.py" , true);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send("Name=Mike");    
      request.onreadystatechange = function()
      {
        if (request.readyState == 4 && request.status == 200)
        {
          document.getElementById("myLabel").innerHTML = "Hello " + request.responseText + "!";
        }
      }
    }
  </script>

nctest.py

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

ANGULARJS无法正常工作(我无法获得姓名的价值(Mike): Angularjs代码:

(function(){
    'use strict'    
    var sectest= {
        controller:sectestCtrl,
        templateUrl:'app/components/component_test/test.html',
    }   

    angular
        .module('myapp')
        .component('secTest',sectest);

        function sectestCtrl($http){
            var prac= this;
            prac.method = 'POST';
            prac.url = 'nctest.py';
            prac.data = {Name : 'Mike'};
            prac.data_answer
            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(prac.data)}).
                    then(function(response) {
                        prac.status = response.status;
                        prac.data_answer = response.data;
                        console.log("OK prac.data_answer: ", prac.data_answer)
                    }, function(response) {
                        prac.data_answer = response.data || 'Request failed';
                        prac.status = response.status;
                    });
            };

        }             
})();

nctest.py代码

#!/usr/bin/python

import json
import cgi
input = cgi.FieldStorage()

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

问题是prac.data_answer会打印空白值。 我已经尝试使用angularjs和python代码的不同标题,但似乎没有工作:

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

您尝试解决的问题有2个。服务器(CGI)&amp;客户端(angularjs)。首先检查您是否通过网络接收数据 - 使用Chrome开发人员工具,在网络标签下。如果是这样,则不需要将Content-Type更改为json,因为默认情况下,angular假设所有http数据都是json格式。

我认为您不需要发布请求的所有属性。当它变得更简单时,它似乎像一个过度杀手。试试这个:

$http.post(url, data).then(function(response){
console.log(response.data);
});