0

I'm facing issue in loading the XML data in Javascript. However if i load the same XML in classic ASP it is working, but if i load the same in Javascript its failing.

Here is the code snippet to load XML in classic ASP & it works fine.

set ObjXMLDom = nothing 
set ObjXMLDom = server.CreateObject("Microsoft.XMLDOM")
ObjXMLDom.async = False

set objSvr = Server.CreateObject("myComMethod.MyComMethod.1")
ObjXMLDom.loadXML(objSvr.GetHierarchyXML()) 'XML loads perfectly fine from server. even if the special character is Dash –
Response.Write(ObjXMLDom.xml) 

code in Javascript to load the XML but it fails for some special characters.

$.ajax("get_xml_from_server.asp", {
                type: 'GET',
                data: { name: groupID, session: sesionID, Employee: empID },
                beforeSend: function () {
                },
success: function (data, status, jqXhr) {

//Data has got the XML string, we can see it by putting alert

alert(data); 

var myXML = new ActiveXObject("Microsoft.XMLDOM");
myXML.async = false;
myXML.loadXML(data); //Here it fails for some special characters like Dash – 


if (myXML.parseError.errorCode != 0) 
      {
          var myErr = myXML.parseError;
          alert("You have error " + myErr.reason + myErr.line + myErr.srcText);
      } 
else  {
          alert(myXML.xml);
      } 
4
  • jQuery can automatically parse xml, if the correct content-type isn't being sent by the asp script then set the dataType property of the options object you pass to $.ajax. Or you can even use DOMParser to parse xml Commented Oct 24, 2018 at 4:06
  • Hi, thanks, i tried the Dom parser, however parser.parseFromString(stringContainingXMLSource, "application/xml"); this line is giving syntax error. any help Commented Oct 24, 2018 at 4:17
  • That just means you have a typo somewhere, just go back over it and check your syntax. It should look something like: parser = new DOMParser; xml = parser.parseFromString(data,'application/xml') where data is the argument from your callback Commented Oct 24, 2018 at 5:03
  • I tried the way you recommended, its failing. is there an difference in loading XML through ASP & Javascript? Commented Oct 24, 2018 at 6:28

1 Answer 1

1

I suspect that there is an encoding problem: the XML file is in one encoding, and the parser is trying to decode it assuming a different encoding, and this results in a failure to decode the non-ASCII characters. You have given us no information about encodings, so this is entirely conjectural. Try to establish

(a) what is the actual encoding of the XML "on disk" on the server

(b) what does the XML declaration at the start of the file (if any) say that the encoding is?

(c) what is the media type and encoding of the HTTP response that delivers the XML payload to the browser?

Chances are, I would guess, that there's something you need to change in your web server / HTTP configuration to ensure that XML files are properly delivered.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes you are right, so to overcome the issue, we need to encode the XML string and send to client. But in the client side we receive the same "Dash –" Character & it breaks. I'm worried about one thing i.e is there an difference in loading XML through ASP or VB & Javascript. Because XML loads fine in ASP but same string if we send to Javascript it breaks.
As I said, I think the difference is that under ASP, the parser is inferring the encoding correctly, and in the browser, it is inferring the encoding incorrectly. I cana't help you more than that without answers to my three questions.
we are not doing any kind of encoding as of now, however i tried to encode using UTF-8,
Hi, i have a query, lets say that we pass a ANSI string to Parser, we can ask the parser to Perform parsing in UTF-8. Cant we do like this?
"ANSI" - do you mean Windows-1252 by any chance (That's a Microsoft proprietary encoding, nothing to do with American national standards institute, but still sometimes referred to as ANSI by old-school Microsofties). I'm not familiar with Microsoft environments, but it's very likely that this encoding will be accepted by Microsoft parsers and not by others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.