2011-10-18 14 views
1

中の例外のうち、メッセージを取得する方法:情報の多くはresponseTextであり私はいくつかのMVCコードを持ってjQueryの

changeTextFormat = function (data) { 

alert(data.responseText); 
alert(data.status); 
} 

:私は、機能を持っている

throw new HttpException(403, "my error text", new Exception("Show me this message")); 

JavaScriptで私がアクセスしたいのは、"my error text"または"Show me this message"です。

responseTextからこれを取得する方法を教えてもらえますか?

答えて

-2

JSONを使用して例外を表示することをお勧めします。

{"error":"my error text","message":"Show me this message"} 

そしてそれを解析:

function (request, statusText, errorMsg) { 
    // use errorMsg to get only the exception message 
}); 

あなたはあなたができる$.post()を使用している場合:あなたのエラー処理方法については、次の引数を使用する場合

function getData(data){ 
data=JSON.parse(data); 
var error=data.error; 
var message=message.message; 
} 
1

は、あなたがメッセージにアクセスすることができますこれを次のように定義します。

$.post(url, data, function (data) { 
    ... success callback ... 
}).error(function (request, statusText, errorMessage) {...}); 

またはOnFailureプロパティを定義してAjaxヘルパー経由でアクションメソッドを呼び出す場合:

// javascript 
var handleError = function (request, statusText, errorMsg) { ... }; 

// view 
@Ajax.BeginForm(..., new AjaxOptions { OnFailure = "handleError" }) 
関連する問題