Angular $ http.post没有发送整个表单

时间:2017-04-04 18:53:37

标签: angularjs perl cgi

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

<!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>

到一个插入MySQL的Perl脚本:

#!/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-&gt; param('POSTDATA')的打印件提供以下内容:

 {"json":"J Doe"}

好的,我找到了名字“J Doe”,以及一些奇怪的“json”键,但电话号码在哪里?
我错过了什么?

2 个答案:

答案 0 :(得分:2)

通过设置$ http.post的标头来输入json:

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

$ cgi-&gt; param(&#39; POSTDATA&#39;)成为

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

从那里开始,Perl的decode_json将字符串转换为一个完成问题的哈希。

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

Stackoverflow又来了。

答案 1 :(得分:1)

之所以发生这种情况是因为您使用2017-04-04 18:57:13.453+0000 INFO [c.g.m.e.s.Searcher] Created Jest Client. 2017-04-04 18:57:13.453+0000 ERROR [c.g.m.e.ElasticSearchWriter] Failed to create Elasticsearch index. org.apache.http.client.ClientProtocolException java.lang.RuntimeException: org.apache.http.client.ClientProtocolException at com.graphaware.module.es.mapping.BaseMapping.lambda$createIndexAndMapping$13(BaseMapping.java:177) at com.graphaware.module.es.mapping.BaseMapping$$Lambda$234/582360661.accept(Unknown Source) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source) at java.util.stream.DistinctOps$1$2.accept(Unknown Source) at java.util.Spliterators$ArraySpliterator.forEachRemaining(Unknown Source) at java.util.stream.AbstractPipeline.copyInto(Unknown Source) ,每当您使用该模式发布时,您需要将已发布网址上的数据添加为查询字符串。

看起来像这样:

x-www-form-urlencoded

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

希望有所帮助。