2017-04-04 43 views
0

一个简单的角度的1.x $ https.post它提交的名称( “ĴDoe的”)和电话号码(1234567):

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> </script> 
    <script> 
    var app = angular.module('myApp',[]); 
    app.controller('myCtrl',function($scope,$http){ 
     $scope.insertData=function(){ 
      $http.post("cgi-bin/in.cgi",{ 
       'bname':$scope.bname, 
       'bphone':$scope.bphone 
       }).then(function(response){ 
        console.log("Data Inserted Successfully"); 
       },function(error){ 
        alert("Sorry! Data Couldn't be inserted!"); 
        console.error(error); 

       }); 
      } 
     }); 
    </script> 
</head> 
<body> 
    <div ng-app="myApp" ng-controller="myCtrl"> 
     <form> 
      Name:- <input type="text" ng-model="bname" /> 
      Phone:-<input type="text" ng-model="bphone" /> 
      <input type="button" value="Submit" ng-click="insertData()" /> 
     </form> 
    </div> 

</body> 
</html> 

要一个Perl脚本,它确实插入到MySQL:被插入到数据库

#!/usr/bin/perl -w 

use strict; 
use warnings; 
use CGI qw(:standard); 
use DBI; 
use POSIX qw(strftime); 
use JSON; 

my $sql; 

my $dbh = DBI->connect('****', '****', '****') || die "Connecting from Perl to MySQL database failed: $DBI::errstr"; 

my $cgi = CGI->new; 
my $name = $cgi->param('bname') || "unknown"; 
my $phone = $cgi->param('bphone') || "unknown"; 

print "Content-type:text/html\r\n\r\n"; 

$sql = "insert into TestDB values ('$name', '$phone')"; 
$dbh->do("$sql") || die ("Connecting from Perl to MySQL database failed: $DBI::errstr"); 

$dbh->disconnect; 

print "Record insert successfully"; 

但唯一的一点是:

+---------+---------+ 
| Name | Phone | 
+---------+---------+ 
| unknown | unknown | 
+---------+---------+ 
1 row in set (0.00 sec) 

的姓名和电话是行不通的,但的$ CGI->参数(“POSTDATA”)打印给出如下:

{"json":"J Doe"} 

OK,我找到了名字,“J李四”,有一些怪异的“JSON”键一起,哪来的电话号码?
我错过了什么?

+0

当你删除|| $ name和$ phone的作业中的“未知”? – Jason

+0

没有|| “未知”执行错误被抛出,因为$ name和$ phone没有设置。显然,$ cgi-> param('name')等不存在。 – user2569618

+0

这听起来像是你的数据类型不匹配。如下面的答案所示,您正在混合表单编码范例。你想用什么格式发送数据? – Jason

回答

2

通过设置$ http.post标题以键入JSON:

header : { 'Content-Type': 'application/json' } 

$ CGI-> PARAM( 'POSTDATA')成为

{ "bname" : "J Doe", "bphone" : "1234567" } 

从那里,Perl的decode_json转换的字符串到哈希完成了问题。

my $decoded = decode_json($cgi->param('POSTDATA')); 
$sql = "insert into TestDB values ('$decoded->{bname}', '$decoded->{bphone}')"; 

来自#1再次通过。

+0

内容类型的AngularJS默认是'application/json'。我很惊讶它还没有发送。你的代码是否覆盖了默认的某个地方?有关更多信息,请参阅[AngularJS $ http API参考 - 设置HTTP头。](https://docs.angularjs.org/api/ng/service/$http#setting-http-headers) – georgeawg

+0

最初,POSTDATA有一些东西看起来像我想要的数据(见原始发布),但直到我按照杰森的建议明确声明头为“application/json”,POSTDATA才包含json格式的所有数据。 – user2569618

1

发生这种情况是因为您使用的是x-www-form-urlencoded,每当您使用该模式后,您需要将发布的网址上的数据添加为查询字符串。

它应该是这样的:

$scope.insertData=function(){ 
     $http.post("cgi-bin/in.cgi?bname=" + $scope.bname + "&bphone=" + $scope.bphone, { 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
      }).then(function(response){ 
       console.log("Data Inserted Successfully"); 
      },function(error){ 
       alert("Sorry! Data Couldn't be inserted!"); 
       console.error(error); 

      }); 
     } 
    }); 

但是,你需要正确编码您发送的值,请参考以下链接了解更多信息: How do I POST urlencoded form data with $http in AngularJS?

希望有所帮助。