XML http request with a URL - javascript

I want to retrieve an XML from a URL and store it in a variable xmlDoc.
I have the following:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","localhost:8080/rest/xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
But I am not getting the XML file, is there something I need to add?

The open method is passing false as the last parameter, making this a synchronous request. The OP's original code is correct, except for one thing: the URL.
xmlhttp.open("GET","http://localhost:8080/rest/xml",false);
Or if you want to make the URL agnostic to the protocol of the current page:
xmlhttp.open("GET","//localhost:8080/rest/xml",false);

before sending, attach a callback like this.
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseText;
}else if (xmlhttp.readyState==4 && xmlhttp.status != 200)
{
alert("error-" + xmlhttp.responseText);
}
}

Related

Wait for XHR request to return 200 and loop

this is my first post here. I've been writing a chat script for a while now, but I've come across an issue. When I use my original code, the browser freezes because it's still trying to load. I have tried almost everything. I'm a bit stumped because I had an idea to clone the function to go back and forth and it STILL won't work. Here is a snippet of what I'm working with.
function jumpanti(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatContent").innerHTML = xmlhttp.responseText;
antifreeze();
}
}
xmlhttp.open("GET", "innerchat.php", false);
xmlhttp.send();
twemoji.parse(document.body);
// Twemoji parse
}
function antifreeze(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatContent").innerHTML = xmlhttp.responseText;
jumpanti();
}
}
xmlhttp.open("GET", "innerchat.php", false);
xmlhttp.send();
twemoji.parse(document.body);
// Twemoji parse
}
I've tried and it still won't work. What I want my code to do is to try and load the chat until it gets a 200 response and then loop. Does anyone have any solutions to this using only XHR?
I fixed the problem. The issue was I didn't have asynchronous XML turned on inside the function. It froze the page a lot. I changed xmlhttp.open("GET", "innerchat.php", false); to xmlhttp.open("GET", "innerchat.php", true);

Javascript get plain text from server

I want to receive a plain text(or any document) from my server as a string variable and for example show it in alert I tried this solution
wordsurl = "http://alpha/test";
function ButtonClicked() {
showsentence(wordsurl)
}
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send(); // NS_ERROR_FAILURE is here
}
function showsentence(generatorurl) {
alert(httpGet(generatorurl));
}
but i'm getting NS_ERROR_FAILURE. Which is referring to the send.
Here is a
How to get plain text from a server?
Here is Server code
Ah, now I see..
You should serve your html page from a http:// server.. Not as a file file://
So setup a simple http server, and try to access it again. You could also serve it from the same server, like your app logic
URL not found.(Is http://alpha/test in your computer?)
Maybe you can change file:// to http://

How to get web url content using Javascript

I used Webrequest in c# to get url Content. Then how to get url content using JavaScript/ Jquery please help to get url content...
WebRequest request = WebRequest.Create ("https://api.pcpartpicker.com/api/2015.1/part/categories/?apikey=XXXXXXXXXXXXXX");//Any Other Google Https Address
WebResponse response = request.GetResponse ();
This code is working and i got data from that url and how we write this code in Javascript
It is not possible to get external webpages content in your site with javascript.
only js,images formats are allowed from external source into your site
You can try this code
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}

Catch every xhr request

I'm trying to achieve $.ajaxSuccess in javascript. I take some refrence from this question. The method I am using works fine when I use it every XHR request but I'm in a situation where there are scripts on the page which I have no control over and I want to be able to hook into any ajax request made by any library (or with vanilla js). So how to attached XMLHttpRequest.prototype.check to document so that we can catch every XHR request. Here is some working
//Script reference taken from w3schools link
XMLHttpRequest.prototype.check=function(){
this.addEventListener("loadend", function(){
alert(0);
}, false);
}
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.check()
xmlhttp.send();
}

$.post not working in ie. Alternative way help please?

I used $.post to send and receive response in my web page. But it is not working in Internet Explorer. I used other way of Ajax. I have to create xmlhttprequest object.
My code is
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
This works fine with all browser except ie-10. I write code like below to support ie-10.
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP.6.0");
}
It is not working with mozila, safari but working with ie-10. I have not checked with ie-7. It is a conflict. Please provide me any help please...........
try going with the first approach and encapsule your send function as shownn below
setTimeout(function () {
xmlhttp.send();
}, 0);

Categories