How to get web url content using Javascript - 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();
}

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://

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();
}

Trigger php file to run

I'm trying to get couple of data from a mysql db.
How do I just trigger a php from a javascript file?
How do I fetch the results back from the javascript file?
Use Ajax to get data from mysql db.
Tutorial
http://www.w3schools.com/ajax/
You can use the script for your requirement:
<script type="text/javascript">
function showInformation()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
mlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
console.log(xmlhttp.responseText);
//this is the result you can get here.
}
}
xmlhttp.open("GET","xxx.php",true);
//xxx.php is the php file you want to fetch data from
xmlhttp.send();
}
</script>
Thanks
Here is the jquery script to do the same operation:
function getInformation(){
$.post('xxx.php',{'param':'..value..'},function(response){
console.log(response);
});
}
Note: before this include the jquery.js file in your script.
Thanks

XML http request with a URL

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);
}
}

Categories