HTML entities not recognized and not displayed when retrieved from ajax - javascript

This happens in all tested browsers (Chrome, Firefox, Opera ...)
Some HTML entities are are swallowed and not displayed when retrieved from ajax. The same HTML entity is displayed when it is hardcoded in the HTML source file.
Here is the actual output: (the entity is not display nether in the web page nor in the console)
Here is the expected output:
Here is the javascript that retrieves the entity:
<html><head><script type="text/javascript"> function injectEntity(){ var xhr = new XMLHttpRequest(); xhr.open("POST", "entity.php", true); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ var doc = xhr.responseXML; console.log(xhr.responseText); var div = document.getElementById("container"); div.appendChild(doc.getElementById("the-entity")); } } xhr.send(null); }</script></head><body> inject the following entity: ’ <div id="container"> </div></body></html>
And here is the php file that is used to retrieve the entity:
<?phpheader('Content-type: application/xml; charset=UTF-8');$xml = new DOMDocument('1.0', 'utf-8');$tag = $xml->createElement('b','’');$tag->setAttribute("id","the-entity");$xml->appendChild($tag);echo $xml->saveXML();?>

You want ’, not ’ (which is an unprintable control character)

Related

ASP.net run javascript in client context

The scenario:
1) User logs in using a browser tab.
2) user opens a new tab and navigates to my web application.
3) my web application executes following javascript to retrieve the user Email from his logged in session:
<script type="text/javascript" >
var xmlHttp = new XMLHttpRequest();
try
{
xmlHttp.open("GET", "https://www.someserver.com/api/getinfo.aspx?EMAIL", false);
xmlHttp.send(null);
}
catch (err) { alert(err.message);}
var data = xmlHttp.responseText;
if (data != "")
{
var json = JSON.parse(data);
alert(json.EMAIL);
}
else { alert(document.body.textContent);}
</script>
4)The problem: if I add this script into the markup of my page.aspx , then run the application with above steps, I fail to retrieve the Email, while if I copy and paste the URL "https://www.someserver.com/api/getinfo.aspx?EMAIL" in a new tab the browser displays the JSON response with Email correctly , what am I missing here?

Cant retrieve an XML list from a web service

I have a server running OTRS 5 and I would like to retrieve a list in XML format. I'm running a JavaScript code that should display the list, but instead I get an error.
My local server is https://labcentos3/otrs/mds.pl?Action=ServiceList.
I think its a Perl script that runs on the server side and then displays a list in XML format.
This is what I get if I browse the local link: it gives me the list I want
I wrote HTML and JavaScript to try to do the same for working with the retrieved data later, but I can't get past an error.
HTML
<html>
<head>
<title>XML read</title>
<script src="reader.js" type="text/javascript"></script>
</head>
<body>
<h1>XML File</h1><br/>
</body>
</html>
reader.js
var user = "bla bla bla";
var pass = "bla bla bla"
var getXMLFile = function(path, callback) {
var request = new XMLHttpRequest();
request.open("POST", path);
request.setRequestHeader("Content-Type", "text/plain");
//request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader('Authorization', 'Basic ' + btoa(user + ":" + pass));
request.onreadystatechange = function() {
if(request.readyState === 4 && request.status === 200) {
callback(request.responseXML);
}
};
request.send();
};
getXMLFile("https://labcentos3/otrs/mds.pl?Action=ServiceList", function(xml) {
console.log(xml);
});
The error I get in the Chrome console is this:
XMLHttpRequest cannot load https://labcentos3/otrs/mds.pl?Action=ServiceList. The request was redirected to 'https://labcentos3/otrs/index.pl', which is disallowed for cross-origin requests that require preflight.

How to get HTML from AJAX Table

I'm using XMLHttpRequest to pull down the HTML from a specified URL, then stored it in fshtml. I then used:
function showInWindow(fshtml) {
var newWindow = window.open();
newWindow.document.write(fshtml);
}
To display all the HTML in a new window. But the HTML does not contain any of the code from the AJAX table in the middle of the page, which is what I want to access. Is there a way to use XMLHttpRequest to include the static version of the AJAX table HTML in xhr.responseText?
EDIT: This code pulls down html from the website:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.flashscore.com/basketball/", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
return showInWindow(xhr.responseText);
}
}
xhr.send();
The new window had HTML for all the headers and tabs and such, but there didn't appear to be any table data. Hope this helps.

document.documentElement function does not display proper result

I am trying to parse XML file using java-script. According to the tutorial I read I found That To get the Root element, I have to use document.documentElement.
I use that syntax but when I tried to display the returned value from that Syntax, the browser displays [object HTMLHtmlElement].
My question is: (1) Why I am getting [object HTMLHtmlElement] displayed in the web browser.
(2) According to the below posted XML-File, What should I expect the output to be
after usingrootElement = document.documentElement;
Please find below the code I used(Javascript) and the XML file.
Javascript
function findWriter()
{
var schriftstellerKnoten, SpracheKnoten;
var FuellerKnoten, DichtungKnoten, Anzeige, rootElement;
rootElement = document.documentElement;
document.write(rootElement);
}
XML file:
<?xml version="1.0" ?>
<Schriftsteller>
<Englischsprache>
<Dichtung>
<fueller>
<name>Jane Austin</name>
<name>Rex Stout</name>
<name>Dashiell Hammett</name>
</fueller>
</Dichtung>
</Englischsprache>
</Schriftsteller>
document.documentElement will get you the root HTML tag of the page in which your javascript is present.
To fetch and display the XML content, you must get access to the XML document and then use it to load the XML file like the following:
function findWriter()
{
var schriftstellerKnoten, SpracheKnoten;
var FuellerKnoten, DichtungKnoten, Anzeige, rootElement;
if (window.XMLHttpRequest){
xhttp=new XMLHttpRequest(); //For non IE browsers
}
else { // for IE 5/6
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","test.xml",false);
xhttp.send();
xmlDoc=xhttp.responseXML; //Fetch the XML file contents
var nameEl = xmlDoc.documentElement.getElementsByTagName("name"); //Get all <name> tags in it
for (i=0; i<nameEl.length; i++){
document.write("Name" + i + ": " + nameEl[i].firstChild.nodeValue + "<br/>"); //write names
}
}

How to display the xml content in the browser

I have a variable in my JavaScript and I have retrieved and stored a huge XML content in this variable like
var content = ""
the content variable will hold huge XML content.
From my same JavaScript file, I am opening a new window using
var mywindow = window.open("\test.html")
and I am using document.write like
mywindow.document.write(content)
to display the stored XML content in the new window.
I am not using any XSLT or any other style sheet in my JavaScript file.
The content is loaded in the window, however the XML content is not loaded in the browser properly, I can see the exact content when I see the source of the page.
How to display the XML content in the browser directly?
try this ,
xmlhttp.open("GET","yourfile.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
<script type="text/javascript">
var xhr= window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.onreadystatechange= function() {
if (this.readyState===4 || this.status===200)
populateTable(this.responseXML);
};
xhr.open('GET', 'yourfile.xml', true);
xhr.send();
function populate(xml) {
var content= xml;
myElem.innerHTML += content;
};
</script>
document.write() will actually create the tags of your XML data rather than displaying it as raw text.
Something like this should work:
var xml = '<hello>world</hello>';
if (document.body.innerText !== undefined) {
document.body.innerText = xml;
}
else {
document.body.textContent = xml;
}

Categories