2017-04-04 89 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?

希望有所幫助。