perl - How to check service success or error in controller in angularJS -
i have function in controller calls service function. code both.
controller
 $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;  }; service
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 script adding product.
#!/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";  $cfg = "config.pl"; $db_handle = dbi->connect ("dbi:mysql:$cfg->{database}", $cfg->{user}, $cfg->{password} ) or die "couldn't connect database: $dbi::errstr\n";  $cgi = cgi->new(); #my $decodeddata = decode_json($cgi->param("newproduct"));  $product_id = $cgi->param('productid'); $product_name = $cgi->param('productname'); $product_description = 'desc'; $image_path = $cgi->param('productimagepath'); $brand_id = $cgi->param('brandid'); $subcatty_id = $cgi->param('subcategoryid');  $sql_query = "insert table_products values ($product_id, '$product_name', '$product_description', '$image_path', '$brand_id', 1)"; $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 table_product_categories values ($product_id, '$subcatty_id')"; $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; i have following issues code.
- sometimes products not getting added db, in service seeing alert ('new product added!'); why so? why not going alert ('new product add error!'); 
- how check success or error in controller instead of service? 
please me.
- may returning http status code - 200after request when not inserting database, if data not insert correctly, need return- $httperror.
- you can return - $httppromise service below,- 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); }
in controller,
productservice.addnewproduct(newproduct).then(function(data) {       // success  }, function(data) {       // error  }); 
Comments
Post a Comment