如何在angularJS

时间:2015-07-23 12:11:55

标签: angularjs perl

我在控制器中有一个调用服务功能的功能。这是两者的代码。

控制器

 $scope.AddNewProduct = function (NewProduct)  {

    var newProduct = {};

    newProduct.ProductID = NewProduct.id;
    newProduct.ProductName = NewProduct.name;
    newProduct.ProductImagePath = NewProduct.imagepath;
    newProduct.SubCategoryID = NewProduct.subcategory.id;
    newProduct.SubCategoryName = NewProduct.subcategory.name;
    newProduct.BrandID = NewProduct.brand.id;
    newProduct.BrandName = NewProduct.brand.name;
    newProduct.Variants = [];

    ProductService.AddNewProduct(newProduct);
    $scope.NewProduct = {};
    $scope.NewProduct.id = parseInt($scope.Products[0].ProductID) + 1;

};

服务

ProductService.AddNewProduct = function(NewProduct)  {

    productList.unshift(NewProduct);
    var req = {
        url: "cgi-bin/AddNewProduct.pl",
        method: 'GET',
        params: {ProductID: NewProduct.ProductID, ProductName: NewProduct.ProductName, ProductImagePath: NewProduct.ProductImagePath, BrandID: NewProduct.BrandID, SubCategoryID: NewProduct.SubCategoryID}
        //params: { NewProduct: ProductData }
    };

    $http(req).success(function()
    {
        alert ('New Product Added!');
    })
    .error(function()
    {
        alert ('New Product Add Error!');
    });

}

用于添加产品的Perl脚本。

#!/usr/bin/perl
use cPanelUserConfig;

use strict;
use warnings;
use DBI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use CGI;
use CGI::Cookie;
use CGI::Session qw();
use JSON;

print "Content-Type: text/html\n\n";

my $CFG = do "config.pl";
my $db_handle = DBI->connect ("DBI:mysql:$CFG->{database}", $CFG->{user}, $CFG->{password} ) or die "Couldn't connect to database: $DBI::errstr\n";

my $cgi = CGI->new();
#my $DecodedData = decode_json($cgi->param("NewProduct"));

my $product_id = $cgi->param('ProductID');
my $product_name = $cgi->param('ProductName');
my $product_description = 'DESC';
my $image_path = $cgi->param('ProductImagePath');
my $brand_id = $cgi->param('BrandID');
my $subcatty_id = $cgi->param('SubCategoryID');

my $sql_query = "insert into table_products values ($product_id, '$product_name', '$product_description', '$image_path', '$brand_id', 1)";
my $statement = $db_handle->prepare ($sql_query) or die "Couldn't prepare query '$sql_query': $DBI::errstr\n";  
$statement->execute() or die "SQL Error: $DBI::errstr\n";  

$sql_query = "insert into table_product_categories values ($product_id, '$subcatty_id')";
my $statement = $db_handle->prepare ($sql_query) or die "Couldn't prepare query '$sql_query': $DBI::errstr\n";  
$statement->execute() or die "SQL Error: $DBI::errstr\n";  

$db_handle->disconnect;

我在代码中遇到以下问题。

  1. 有时产品没有被添加到数据库中,但在服务中我看到警报('新产品已添加!');为什么会这样?为什么它不会发出警报('新产品添加错误!');

  2. 如何检查控制器中的成功或错误而不是服务?

  3. 请帮帮我。

2 个答案:

答案 0 :(得分:0)

  1. 请求后,当它未插入数据库时​​,可能是您返回的http状态代码200,如果数据未正确插入,则需要返回$http错误。< / p>

  2. 您可以从服务中返回$http承诺,如下所示

    ProductService.AddNewProduct = function(NewProduct)  {
    
        productList.unshift(NewProduct);
        var req = {
              url: "cgi-bin/AddNewProduct.pl",
              method: 'GET',
              params: {ProductID: NewProduct.ProductID, ProductName: NewProduct.ProductName, ProductImagePath: NewProduct.ProductImagePath, BrandID: NewProduct.BrandID, SubCategoryID: NewProduct.SubCategoryID}
              //params: { NewProduct: ProductData }
         }; 
    
         return $http(req); 
    
    }
    
  3. 在控制器中,

    ProductService.AddNewProduct(newProduct).then(function(data) {
    
         // success
    
    }, function(data) {
    
         // error
    
    });
    

答案 1 :(得分:0)

$http能够使用响应数据:

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

让您的API cgi-bin/AddNewProduct.pl返回成功/错误/状态数据。 console.log查看它返回的内容,以便您可以调试。