Page view counter

Gibberish Returned in Strings from Web Service

Last post 09-18-2008 10:33 PM by bullines. 6 replies.

Sort Posts:

  • Confused [*-)] Gibberish Returned in Strings from Web Service

    09-05-2008, 4:23 PM
    • Loading...
    • bullines
    • Joined on 08-08-2007, 11:18 AM
    • Posts 82
    • Points 16

     Greetings!

     I have a web service (ASMX) and in it, a web method that does some work and throws an exception if the input wasn't valid.

    throw new Exception(l_errMsg ?? "Invalid Input.");
    Back in the client script, on the error callback function, I display my error:
    function GetInputErrorCallback(error)
    {
    // Insert the error message into the Forgot Password help window's existing content.
    var errorDiv = document.getElementById('input_error');

    if (errorDiv != null)
    errorDiv.innerHTML = error.get_message();
    }

    This works great.  However, if one of my error messages that's returned contains a special character, it's displayed incorrectly in the browser.

    For example, if the error message were to contain the following:

    That input isn’t valid!  (that's an ASCII #146 in there)

    It displays this:

    That input isn’t valid!

    Or:

    Do you like Hüsker Dü? (ASCII # 252)

    Becomes:

    Do you like Hüsker Dü?

    The values returned by the web service (in the l_errMsg variable) look fine.  It's just once the client script has a hold of, it displays incorrectly.  How can I fix this?  Thanks in advance.

  • Re: Gibberish Returned in Strings from Web Service

    09-05-2008, 4:49 PM
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 2:26 PM
    • Mexico City, Mexico
    • Posts 640
    • Points 4,324

    I don´t think there’s a problem with your webmethod; I think you should set the correct encoding of your page so your client can see the special symbols.

    If this post helped you please remember to set it as Answer so it can help others.
  • Re: Gibberish Returned in Strings from Web Service

    09-05-2008, 5:33 PM
    • Loading...
    • bullines
    • Joined on 08-08-2007, 11:18 AM
    • Posts 82
    • Points 16

    By "page encoding" are you referring to setting the character set via a manner such as this?

    Response.Charset = "Windows-1252";

    Thanks in advance.
  • Re: Gibberish Returned in Strings from Web Service

    09-05-2008, 5:52 PM
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 2:26 PM
    • Mexico City, Mexico
    • Posts 640
    • Points 4,324

    You'll need to set the encoding with something like:

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");

    In my case iso-8859-1 works great for Spanish special symbols.

    If this post helped you please remember to set it as Answer so it can help others.
  • Re: Gibberish Returned in Strings from Web Service

    09-06-2008, 7:08 PM
    • Loading...
    • bullines
    • Joined on 08-08-2007, 11:18 AM
    • Posts 82
    • Points 16

    In my Page_Load() method, I've tried:

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

    and:

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252");

    But no luck with either. My error messages that I return from my web service are retrieved from XML files that are encoded with UTF-8:

     <?xml version="1.0" encoding="UTF-8"?>

    Is there anything else that I could be missing?  Thanks in advance.

  • Re: Gibberish Returned in Strings from Web Service

    09-13-2008, 2:13 PM
    • Loading...
    • bullines
    • Joined on 08-08-2007, 11:18 AM
    • Posts 82
    • Points 16

    To add, I've noticed that the string only becomes gibberish when it's returned from a thrown exception.  For example, here's a sample web method in my ASMX:

    [ScriptMethod]
    [WebMethod]
    public string MyWebMethod(string input)
    {
    if (!ValidInput(input))
    {
    string l_msg = GetErrorMessage();
    throw new Exception(l_msg);
    }
    else
    return
    input;
    }

    Back on a web page, I may have something like this:

    function GetUserInput()
    {
    MyWebServices.MyWebMethod(user_input, OnSuccessCallback, OnFailCallback);
    }

    function OnSuccessCallback(result)
    {
    $get('msg_area').innerHTML = result;
    }

    function OnFailCallback(error)
    {
    $get('err_msg').innerHTML = get_message();
    }

    If the Web Service succeeds and the OnSuccessCallback function is invoked in the client-side JavaScript, any text with characters (such as "ü", which is ASCII #252), it's displayed properly - no problem.  However, web the Web Service has to throw an exception, thus invoking the OnFailCallback function in the client-side JavaScript, then those same characters are garbled.  Is there something I'm missing?  Thanks in advance.

     

  • Re: Gibberish Returned in Strings from Web Service

    09-18-2008, 10:33 PM
    Answer
    • Loading...
    • bullines
    • Joined on 08-08-2007, 11:18 AM
    • Posts 82
    • Points 16

    I managed to find a solution.  I neeed to set the ContentEncoding of the Response to match that of the XML files I was retrieving the message from (UTF-8):

     

    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
      

    Thanks.

Page 1 of 1 (7 items)