Invalid root element name 'HTML'. "root" is the only permitted root element name

I am using msdn example code and it has jsonp shell files, you can find the code here

This article and MSDN JSON articles with Padding (AJAX)

but when I run the code, it throws me this error:

Encountered invalid root element name 'HTML'. 'root' is the only allowed root element name

what does it mean?

enter image description here

+4
source share
3 answers

This means that you made some kind of web request that expects to receive some XML data, but instead it returns HTML data. A common reason is a confused URL. If your URL was right then XML will be returned as expected. Since this is corrupted, you get HTML (possibly a page with an error).

Check your URLs to make sure they are correct.

+5
source

I found a solution to a similar problem. In my case, I got a similar error when my service returned raw JSON, i.e. it returned Stream, which represented this JSON.

Error: An invalid root element name 'Binary' was detected. "root" is the only resolved name for the root element.

The problem is that the example provided by MS uses JsonWriter to convert the message to JSON, but this writer expects your message to be composed of JSON objects that it can convert to Stream. In my case, the message was binary data, so instead of a single β€œroot” element, I had a β€œBinary” element.

I overcame this problem by modifying the classes provided by the MS sample. I basically check the message format - if it is JSON, I can still use JsonWriter, if it is binary, I need to take a different approach. In your case, the message is in HTML format (I'm not sure how you service it), but you will find another way to get the text of the message.

I wrote a blog post about my problem here: http://hoonzis.blogspot.com/2011/07/provide-jsonp-with-your-wcf-services.html

Hope this helps, Honza

+2
source

I came across the same error message, but in a different scenario. I added JSON support to the WCF web service, which only supported XML.

In particular, I wanted to return an error message object in JSON. I had a class that executed System.ServiceModel.Dispatcher.IErrorHandler . In the ProvideFault method ProvideFault I set `WebBodyFormateMessageProperty to the appropriate one wither, XML or JSON based on the fact that it was passed in the accept header. I also set up the content type. What I was missing was using the right serializer for every case

  Dim webBodyFormatMessageProp As Channels.WebBodyFormatMessageProperty Dim contentType As String Dim serializer As XmlObjectSerializer If WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json Then webBodyFormatMessageProp = New System.ServiceModel.Channels.WebBodyFormatMessageProperty(System.ServiceModel.Channels.WebContentFormat.Json) contentType = "application/json" serializer = New DataContractJsonSerializer(GetType(MyErroClass)) Else webBodyFormatMessageProp = New System.ServiceModel.Channels.WebBodyFormatMessageProperty(System.ServiceModel.Channels.WebContentFormat.Xml) contentType = "text/xml" serializer = New DataContractSerializer(GetType(MyErroClass)) End If Dim detail = faultException.[GetType]().GetProperty("Detail").GetGetMethod().Invoke(faultException, Nothing) fault = System.ServiceModel.Channels.Message.CreateMessage(version, "", detail, serializer) fault.Properties.Add(System.ServiceModel.Channels.WebBodyFormatMessageProperty.Name, webBodyFormatMessageProp) Dim httpResponseMessageProp = New System.ServiceModel.Channels.HttpResponseMessageProperty() httpResponseMessageProp.Headers(System.Net.HttpResponseHeader.ContentType) = contentType httpResponseMessageProp.StatusCode = System.Net.HttpStatusCode.OK httpResponseMessageProp.StatusDescription = [error].Message fault.Properties.Add(System.ServiceModel.Channels.HttpResponseMessageProperty.Name, httpResponseMessageProp) 

Sorry for VB.net, but I'm working on it now.

0
source

Source: https://habr.com/ru/post/1342678/


All Articles