一個簡單的角度的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」鍵一起,哪來的電話號碼?
我錯過了什麼?
當你刪除|| $ name和$ phone的作業中的「未知」? – Jason
沒有|| 「未知」執行錯誤被拋出,因爲$ name和$ phone沒有設置。顯然,$ cgi-> param('name')等不存在。 – user2569618
這聽起來像是你的數據類型不匹配。如下面的答案所示,您正在混合表單編碼範例。你想用什麼格式發送數據? – Jason