2017-07-31 13 views
1

美好的一天!我想問一下如何用單引號過濾產品名稱。例如,我想搜索「歐萊雅」品牌名稱,因爲單引號而導致錯誤。有人可以幫我解決這個問題嗎?以及如何將其插入控制器?謝謝!如何在控制器中插入單引號來過濾產品名稱

this is my error

這是我在我的控制器檢索碼

$scope.filterSearch2nd = function(sid, bname, bcode, gnder, ptype, pname, size_oz, size_ml, size_g, size_pcs,scpidlist, brandnamelist, productnamelist) { 


    //console.log($scope.brandnamelistvalue.length); 
    brandnamelist=""; 

    if(bname =="" && $scope.brandnamelistvalue.length<1) { 
     bname ="brandname"; 
    } 
    else if($scope.brandnamelistvalue.length==1){ 

     bname=$scope.brandnamelistvalue[0]; 
    } 
     else{ 
     bname="list"; 
     for(var i=0;i<$scope.brandnamelistvalue.length;i++){ 
      //console.log($scope.brandnamelistvalue[i]); 
      if(i==0) 
       brandnamelist+=$scope.brandnamelistvalue[i]; 
      else 
       brandnamelist+=","+$scope.brandnamelistvalue[i]; 
     } 

    } 

    $rootScope.loader = true; 
    skinCareProdService.filter2ndSearch(sid, bname, bcode, gnder, ptype, pname, size_oz, size_ml, size_g, size_pcs,scpidlist, brandnamelist, productnamelist) 
     .then(function(data){ 
      $scope.rawSCP = data.data; 
      $rootScope.loader = false; 
     } 
    ); 

} 
    $("#search2nd").click(function(){ 


}); 

這是我在服務

function filter2ndSearch(list, bname, bcode, gnder, ptype, pname, size_oz, size_ml, size_g, size_pcs,scpidlist, brandnamelist, productnamelist){ 
    return $http({ 
     method: 'post', 
     url: rootURL + '/2ndlayer/search/' + list + '/' + bname + '/' + bcode + '/' + gnder + '/' + ptype + '/' + pname , 

     data: $.param({ 
      size_oz: size_oz, 
      size_ml: size_ml, 
      size_g: size_g, 
      size_pcs: size_pcs, 
      scpidlist: scpidlist, 
      brandnamelist: brandnamelist, 
      productnamelist: productnamelist 
     }), 
     headers: { 
      'Content-Type': 'application/x-www-form-urlencoded' 
     } 
    }); 
} 
+0

你可以分享你的代碼,你做了什麼?你得到什麼錯誤?分享所有這些細節。 – VicJordan

+0

已更新@VicJordan –

+0

看起來你不是URL編碼HTTP請求中使用的值。顯示你的skinCareProdService.filter2ndSearch方法的代碼。 – Phil

回答

1

你使用任何字符串搜索代碼在URL中應該正確編碼。您通常可以使用encodeURIComponent函數,但這不包括您需要的單引號。請嘗試以下

var path = [list, bname, bcode, gnder, ptype, pname] 
    .map(encodeURIComponent) 
    .join('/') 
    .replace(/'/g, '%27') 

然後在$http電話...

url: rootURL + '/2ndlayer/search/' + path 

我強烈建議你在看它支持URL路徑參數和處理任何編碼問題對您角的ngResource模塊

+0

好的,我會試試這個,我認爲它現在正在工作,我只需要一點修改,非常感謝你!很好的幫助 –

+0

@ChristinejoyDeGuzman我剛剛意識到這是行不通的。我更新了我的回答 – Phil

+0

encodeURIComponent(bname).replace(「'」,「''」)這個也是可以的 –

相關問題