2

I am using the following:

var items = $.map(json.errors, function (error) {
return error + '</br>';
}).join(''); 

json.errors can be IEnumerable<string> or just string.

When json.errors is IEnumerable then items is populated with text with a <br> between each error message. However when json.errors is a string then items is populated with the error string with a <br> after every character of the string.

Is there some way that I could fix the problem for when json.errors is just a plain string?

4

2 に答える 2

2

最初に の型をテストjson.errorsし、文字列でない場合にのみ内容の変更に進みます。次を使用してタイプを取得できますtypeof

if ( typeof json.errors === "string" ) {
  // Proceed understanding you're handling a string
}
于 2012-05-20T06:00:51.630 に答える
2

現在のコードは、文字列<br/>だけでなく、すべての文字列の後にa を追加するため、これは同等になります。

if (typeof json.errors === "string" ) {
    json.errors = [ json.errors ] ;
}

// continue as before
于 2012-05-20T06:02:56.400 に答える