2

I am making an ajax call to a service that returns html as a string with this code..

var xhReq = new XMLHttpRequest();
xhReq.open("POST", "MobileClientService.svc/REST/TestHtmlSend", false);
xhReq.send(null);
xhReq.responseType = "text/html";
var serverResponse = xhReq.responseText;
alert(serverResponse); // Shows "15"

The service creates the html correctly..

<div data-bb-type="item" data-bb-img="Images/imagesBBUI/icons/icon11.png" data-bb-title="Title From Server">

</div>

The problem is "serverResponse" in my code is returning this..

<div data-bb-type=\"item\" data-bb-img=\"Images/imagesBBUI/icons/icon11.png\" data-bb-title=\"Title From Server\">
  \u000d\u000a\u000d\u000a
<\/div>

Here is the C# code used to create the html..

  public string TestHtmlSend()
        {
            string moomoo = String.Empty;

            // Initialize StringWriter instance.
            StringWriter stringWriter = new StringWriter();

            // Put HtmlTextWriter in using block because it needs to call Dispose.
            using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
            {
                //start collapse div
                writer.AddAttribute("data-bb-type", "item");
                writer.AddAttribute("data-bb-img", "Images/imagesBBUI/icons/icon11.png");
                writer.AddAttribute("data-bb-title", "Title From Server");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                //writer.Write("Some Whatever Description");
                writer.RenderEndTag();

            }

            moomoo = stringWriter.ToString();

How do I change my code to return the html as it is without all the extra "\"?

6
  • Can you use jquery's $.ajax()? Commented Sep 28, 2012 at 20:15
  • 2
    @TravisJ Why does everyone always default to jQuery. Maybe hes trying to learn what the javascript is doing in the background or cannot use libraries? Commented Sep 28, 2012 at 20:17
  • @locrizak - Because it is standardized and using a plain XMLHttpRequest can have issues with certain browsers. Commented Sep 28, 2012 at 20:19
  • I cant use jquery. It works with jquery but I cannot use it Commented Sep 28, 2012 at 20:34
  • What is the character encoding that is being returned with the Ajax response? Can you give us a sample of the headers coming back with that Ajax response? Commented Sep 28, 2012 at 20:56

1 Answer 1

1

I've run into weird issues like this before and it usually always boils down to the middleware.

Looks like a variable interpolation issue on the server side.

What is your middleware language and/or framework?

Ensure that you are printing to your output stream in a clean manner:

  • using only a single variable (where you can easily see quote interpolation)
  • not escaping on your output, unnecessarily
Sign up to request clarification or add additional context in comments.

2 Comments

btw, the unicode characters in there might be a sign that you are parsing your string through an html entities translation ... maybe an html_entities_decode is the source of your problems?
I updated the question with the C# code used to create the html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.