ajax - ASP.NET WebService Returns Gibberish Characters When Throwing Exceptions -
i have web service (asmx) , in it, web method work , throws exception if input wasn't valid.
[scriptmethod] [webmethod] public string mywebmethod(string input) { string l_returnval; if (!validinput(input)) { string l_errmsg = system.web.httputility.htmlencode(geterrormessage()); throw new exception(l_errmsg); } // work gets done... return system.web.httputility.htmlencode(l_returnval); }
back in client-side javascript on web page, on error callback function, display error:
function getinputerrorcallback(error) { $get('input_error_msg_div').innerhtml = error.get_message(); }
this works great , when web method returns (a string), looks perfect. however, if 1 of error messages thrown exception contains special character, it's displayed incorrectly in browser. example, if error message contain following:
that input isn’t valid! (that's ascii #146 in there)
it displays this:
that input isn’t valid!
or:
do hüsker dü? (ascii # 252)
becomes:
do hüsker dü?
the content of error messages comes xml files utf-8 encoding:
<?xml version="1.0" encoding="utf-8"?> <errormessages> <message id="invalid_input">your input isn’t valid!</message> . . . </errormessages>
and far page encoding concerned, in web.config, have:
<globalization enableclientbasedculture="true" fileencoding="utf-8" />
i have http module set l10n parameters:
thread.currentthread.currentuiculture = m_selectedculture; encoding l_enc = encoding.getencoding(m_selectedculture.textinfo.ansicodepage); httpcontext.current.response.contentencoding = l_enc; httpcontext.current.request.contentencoding = l_enc;
i've tried disabling http module result same.
the values returned web service (in l_errmsg variable) fine in vs debugger. it's once client script has hold of, displays incorrectly. i've used firebug @ response , special characters mangled in there, too. find pretty strange strings returned web method fine, if there's special characters in them. yet when throw exception web method, special characters in message incorrect. how can fix this?
are sure setting "fileencoding" want, , not "responseencoding"? setting fileencoding determines how web server try read physical .asmx/.aspx files disk when can't determine encoding automatically. so, settings "utf-8" means must save .asmx/.aspx files in utf-8. don't think relevant though.
the mangling you're seeing when text encoded utf-8 parsed using 8-bit encoding (i.e. utf-8 bytestream decoded using 8-bit decoder, such as, in case, iso-8859-1/windows-1252). it's possible htmlencode() you're doing before throw()ing exception wrong intended output encoding. happens if don't htmlencode() error message?
(technically, "ascii # 252" isn't quite right; ascii has 128 characters; apostrophe use coming 8-bit encoding such as, in case, iso-8859-1/windows-1252.)
are sure you've disabled http module correctly? line looks causing problem:
httpcontext.current.response.contentencoding = l_enc;
...since it's setting output encoding 8-bit encoding (the ansi code page equivalent).
to support many cultures possible, should set response encoding utf-8. supported unicode format in browsers (i daresay modern browsers support it), , unicode alternative local encodings. said, don't understand http module using , why need it, situation may more complex think.
Comments
Post a Comment