How do i retrieve the data from this xml page - javascript

So im trying to get all data to display back from the request but i cant get a response it just keeps giving me [object XMLDocument] or blank if i use .responseText
This is the request URL http://www.omdbapi.com/?t=Chef&plot=full&r=xml
This is the site http://www.omdbapi.com/
This is the code i am using
<button onclick="loadXML()">Run</button>
<p id="output"></p>
<script>
function loadXML() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("output").innerHTML = this.responseXML;
}
};
xmlhttp.open("GET", "http://www.omdbapi.com/?t=Chef&plot=full&r=xml", true);
xmlhttp.send();
}
</script>
All i need is a response returned and i can go from there but nothing i try is working :/

Thankyou for looking but i figured it out. I was receiving HTML tags and when i was outputting these tags they wouldnt show because they were echoing as HTML tags. The data was there just hidden. To resolve this i just saved the file instead, or it could be cleaned before outputting

Related

Input stops working when I change value (Node.js)

I just want to insert value in text input on https://hordes.io/clans website.
When you natively enter there characters, all elements load automatically. But when I do this:
document.getElementsByClassName("navbtn")[0].value += "d"
It neither loads new results nor works at all when you try to do anything manually afterwards.
In case with document.getElementsByClassName("navbtn")[0].value += "" it continues working.
I tried another method to enter values there: clicking input field - returns undefined.
Is there any way I can search on this page? By the way I did it just in chrome snippet
I think I've found a decent solution. Their website makes a post request to their api --> https://hordes.io/api/claninfo/list.
You can pass json to specify which clan you want!
Like this:
POST: https://hordes.io/api/claninfo/list
PAYLOAD: {"name":"dmdw","order":1}
RESPONSE:
{"clans":[{"name":"Darkmetal Dwarfs","tag":"DmDw","level":9,"gold":"42141","faction":0,"members":"2","prestige":"9446"}]}
You can try the request on reqbin.com following this link with already filled in params: https://reqbin.com/oieogjuu
Generated code from reqbin.com:
var url = "https://hordes.io/api/claninfo/list";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};
var data = '{"name":"dmdw","order":1}';
xhr.send(data);

producing a GET response to an ajax POST

In javascript I am POSTing with ajax and I need to get a reply from my Python program. The following code shows the javascript, but how does I produce the desired responseText from a GET on the Python side?
In this example I am want a Yes or No reply to a question for the user as to whether to change the name provided or not.
I understand how to receive the name and value in the python POST, but I don't know how to produce the response in the associated python GET.
function changeName(name,value){
var json = '[{';
json += '"name":"'+name+'",'
json += '"value":"'+value+'"}]'
loadDoc(json,"edit")
}
function loadDoc(data,url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", ("/"+url), true);
xhttp.setRequestHeader("Content-type", "text"); #or something
xhttp.send(data);
}
My typical response in Python GETs follows one of the following 2 patterns, but neither seems to allow me to reply in this case.
self.response.out.write(template.render( template_values))
return webapp2.redirect("/templatename/%s" % variable)
(No PHP or jquery, please.)

Get innerHTML from XMLHttpResponse

I'm trying to write a chrome extension that will get a value from a current page, then use that information to go to another page and pull a specific element from the html response. I can get the html response fine, but I'm unable to parse the html response to get the specific element.
content.js
function getTicketInfo(){
var ticketURI = document.getElementById("p3_lkid").value;
var ticketNumber = document.getElementById("p3_lkold").value;
var xhr = new XMLHttpRequest();
xhr.open('GET',"remotePage.html",true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
function handleResponse(xhr)
}
}
xhr.send();
}
function handleResponse(xhr){
var contactElement = xhr.getElementById("CF00N80000005MAX6_ileinner");
alert(contactElement.clildNodes[0].nodeValue);
}
remotePage.html
<html>
<div id="CF00N80000005MAX6_ileinner">
Text I need!
</div>
</html>
How can I get this value from the external page? Is there a better way to request this information?
Your XHR response is a string, and not a DOM.
With jQuery you'll be able to convert it to a DOM, and query it.
function handleResponse(xhr){
$(xhr.response).find('#CF00N80000005MAX6_ileinner')
}
This is as simple as not parsing the HTML response to a DOM object. According to MDN, this is how you parse XML (Or HTML, and with Vanilla JavaScript):
var parser = new DOMParser();
var doc = parser.parseFromString(xhr, "text/xml");
And then using the new DOM Object doc for accessing elements.
var contactElement = doc.getElementById("CF00N80000005MAX6_ileinner");
alert(contactElement.childNodes[0].nodeValue);
I also noticed you spelled childNodes wrong, but that isn't the main problem.

How to write a JavaScript which returns the contents of a webpage as result when the URL is given

I want to write a JavaScript to return contents of a webpage when URL is given and enter those contents as a data in table?
The alert pop up is not working.
Return HTML content as a string, given URL. JavaScript function returns a blank screen.
Here is my code:
<html>
<head></head>
<body>
<script type="text/JavaScript"> function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
document.write(httpGet("stackoverflow.com/")); </script>
</body>
</html>
You can simply get contents of a webpage by using javascript's ajax
var contents="";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
contents=this.responseText;
}
};
xhttp.open("GET", "webpage.html", true);
xhttp.send();
Although you can also manipulate HTML you should make sure that webpage.html is showing the data in a valid format for you to easily manipulate. Like JSON, xml or anything else so that you can use the data in the contents variable and iterate over it for your insert statements.
The reason because its showing a blank screen is because of the cross origin policy. stackoverflow.com does not allow CORS(cross origin requests).
And one more thing,, Until and unless you are requesting from a relative Url, specify the address with the protocol name.
Like : 'stackoverflow.com' to 'http://stackoverflow.com'
So just change the address to your file and it will work fine. :)

Get DOM from a string

Given a String which contains HTML for an entire page, I want only the innerHTML of the body. Rather than parse the HTML myself, it seems easier if I could make an element from the String, and then just get the body directly.
I found some things related, but that I couldn't get to work (I can't find the question anymore).
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
var ret = xmlhttp.responseText + "";
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(ret);
}
}
xmlhttp.open("GET", "http://url.php", false);
xmlhttp.send();
Right now I have this ajax request, but I need only the body from the return.
I tried document.createElement(ret).body and new Element(ret).body but they didn't seem to work.
var helper = document.createElement("html");
helper.innerHTML = ret;
body = helper.querySelector("body"); //Or getElementsByTagName("body")[0]
You could use simple_html_dom to do this, and get the HTML of the entire page using PHP, and then get only the contents of the body, like this
$html=file_get_html("url.php");
$body=$html->find("body");
$echo $body->plaintext

Categories

Resources