2017-02-10 71 views
0

您好我正在做與web api作爲前端的angularjs應用程序。當某些用戶沒有權限某些我回到Httpresponsemessage作爲如何在angularjs中顯示錯誤消息(404)

var message = string.Format("You do not have permission"); 
         HttpError err = new HttpError(message); 
         return Request.CreateResponse(HttpStatusCode.NotFound, err); 

我想在角提醒上述消息,我下面想。

$scope.Update = function() { 
     var servCall = ProjectSetting_Service.update(sub); 
     servCall.then(function (data) { 
      alert(JSON.stringify(data)); 
      }); 
    }, function (error) { 
//How to alert message here 

    } 

我可以在這裏得到一些幫助!任何幫助,將不勝感激。謝謝。

+0

嘗試'的console.log( '錯誤',錯誤);在'你的errorCallback函數並顯示響應,你可以這樣做alert(error.data.message);' – nivas

回答

5

添加在你的HTML和顯示一種觀點認爲,在錯誤查看試試這個

$scope.showError = false; 
$scope.Update = function() { 
     var servCall = ProjectSetting_Service.update(sub); 
     servCall.then(function (data) { 
      alert(JSON.stringify(data)); 
      }); 
    }, function (error) { 
    //alert message 

    $scope.showError = true;  
    } 

在HTML

<div ng-show="!showError "> 
    <p> Everything is going fine :)</p>  
</div> 
<div ng-show="showError "> 
    <p> 404 sorry</p> 
</div> 
+0

alert(error.statuscode)這樣的東西我可以嘗試提醒404? –

+0

你可以但不是最好的解決方案提醒,嘗試添加一些自定義的html代替404,https://www.google.co.in/adsensehfhskdfkjhsdf點擊鏈接並檢查一個例子 –

+0

是的,我知道但我只是編寫服務器端代碼和前端正在做別人。測試紫袍我想在警報中顯示404。 –