Getting html data from wikipedia api using javascript - javascript

I am trying to log data to my console but I keep getting an error, what might be wrong with the code? Actually trying to display the html content on a page. Here is my snippet
$(function () {
$.ajax({
url: '//en.wikipedia.org//w/api.php?action=parse&format=json&page=pizza&prop=text&section=0&contentmodel=wikitext&formatversion=2',
dataType: 'jsonp',
success: function (data) {
console.log(data);
}
});
});

net::ERR FILE NOT FOUND error message
Your URL starts with // so it uses whatever scheme the URL of the HTML document the JS is running in uses.
If you load the HTML document over HTTP then you try to access Wikipedia over HTTP.
If you load the HTML document over HTTPS then you try to access Wikipedia over HTTPS.
If you load the HTML from a local file, then you try to access Wikipedia from your own hard disk … where it doesn't exist.
Use an absolute URL or, better, test your webpages on a web server.

Related

How to download multiple HTML pages to dropbox with apify using jquery ajax

I'm using Apify to crawl about a hundred pages, and I wish to download the HTML files of all the pages I visit into a dropbox folder. How do I specify this in my jQuery Ajax code?
Sorry in advance, I am quite new to Javascript and everything web-related.
I've tried to follow these tutorials already: https://forum.apify.com/t/data-download-from-within-crawler/48 and https://www.dropbox.com/developers/documentation/http/documentation#files-save_url, however, I am only able to download the HTML file of the second page I visit. I know that my crawler works perfectly fine and visits all the sites it needs to, as I am getting the results I need from these pages, so the problem seems to be that I am not specifying that I want to download all the HTML files. How do I do this?
(In my actual code I have written in the correct Oath-token, I just don't want it to be available online for everyone to see)
var html = $('html').html();
var url = "https://content.dropboxapi.com/2/files/upload";
$.ajax({
url: url,
contentType: "application/octet-stream",
headers: {
"Authorization": 'Bearer ' + 'My Oath-token',
"Dropbox-API-Arg": "{\"mode\":\"add\",\"path\":\"/a.txt\"}",
},
type: 'POST',
data: html,
autoRename: true,
max_results: 1000
});
What I am getting out of this is one file saved as a.txt in my dropbox, which is what I wanted, only that this file only includes one HTML file, not a file including all the files my crawler had visited.
This code is the first thing my crawler meets for every new page it visits.

Ajax url GET 404 error

I'm trying Ajax on loading all pictures inside one local folder onto my html page. Code references this question. Files run on (Tomcat 8.5) server in Eclipse first and I open url in Google Chrome. Then Ajax fails according to the console:
GET /Users/jiaqni/.../WebContent/upload 404 ()
Any idea what I did wrong? Relative path "dir='upload/';" neither works. Thanks guys!
<script>
$(document).ready(function(){
console.log("Image appending...");
var dir = "/Users/jiaqni/.../WebContent/upload/";
var regexp = new RegExp("\.png|\.jpg|\.jpeg");
$.ajax({
url: dir,
success: function (data) {
//List all .png .jpg .jpeg file names in the page
console.log("Success!");
$(data).find("a").filter(function(){return regexp.test($(this).text());}).each(function(){
var filename = this.href.replace(window.location, "");
...
});
}
});
});
</script>
.htaccess was added to folder /User/.../upload/ to ensure it's browsable. And without Ajax, <img src="upload/xxx.jpeg"/> does display image in that folder.
I am guessing that the URL in question here refers to a local resource on your computer.
Unfortunately, this is not possible - usually browsers (e.g., Google Chrome) prevent you from doing so (due to privacy & security issues that may arise by allowing it).
You should put your files in your web server (e.g., Apache, ngnix, etc.) and adjust the URL of the AJAX request accordingly.
Good luck.

Refresh another page

I have an application that sends web requests to a .php file that then creates a flatfile database with the web requested data. Another .php file loads the data from the flatfile and creates a mock css file loadout just outside of the php code. An HTML file then treats the .php file as a .css file.
Quick pseudo of the php that acts as css:
<?php
header("Content-type: text/css");
load stuff1;
load stuff2;
?>
p{font-size: <?=stuff1?>;}
h{font-size: <?=stuff2?>;}
Well, you get the idea right? Basically, what I want to do is to refresh the html file whenever this mock css file written in php is changed. The application that I'm using constantly is sending data to my first php file, and I want to refresh the html file whenever the mock css file is updated. Any ideas on how I can sync this up? I also don't know how to refresh an external page. Any help would be appreciated!
Edit: One step further. If I could somehow update the css without refreshing the page, that would be a huge bonus too!
You need to set a date or cache thing to check if the file is newer than it was before. You can check once a while with AJAX if the file is modified and if the result is true you can refresh your page with jquery or javascript.
$.ajax({
url : "URL_TO_FILE",
type: "GET",
data : formData,
success: function(data, textStatus, jqXHR)
{
//data - response from server
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});
Here is a link so you can change the css file.
http://www.sitepoint.com/jquery-change-css-file-2/

Ajax put to add XML entries into XML file

I am trying to add some emails taken from an inputbox into a .txt file present on my webserver. Here is the code :
email = document.getElementById("mail").value;
$.ajax({
url: 'maillist.txt',
datatype: 'text',
type: 'PUT',
data: email + '; ',
success: function(data) {
alert('Should have work yay!');
}
});
but that doesn't work on any browser. :(
I have tried using javascript classic methods but it was a no go as well...
I would need either a PUT or POST method, either jQuery or JS, to be able to do this on internet explorer 8 and up as well as firefox and chrome. Emails should appear in the text file as
email1#cooldomain.com; email2#cooldomain.com; .....
Just so it works with our in-house VBA Macro. :)
Also, could there be a method for dropping data into XML files (aka create a new XML entry with form data)? And also, is it possible to upload a file from client side to server side using jQuery? Because i would need users to fill up forms and drop their data into an XML file, and link up a file they choose with that. That way they could add stuff into the XML themselves and they would show up brand new into the webpage.
Kindoff "reddit" or "4chan" like if you know the references.
Thanks for your time, really appreciated!
You can't post from a browser to a text file on the server side. You need to have some sort of code on the server side that will receive the HTTP PUT, and persist the data to a file local to the server.

JS file in script tag's src attribute downloaded but not executed(from ajax call)

I am invoking an ajax call from jquery like this:
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
container.html(data);
},
});
The data recevied from the ajax call contains script tags that reference other JS files, for ex: angularjs. Wehn I look at firebug I see that the JS files are downloaded but they do not appear in the Script tab one could debug it.
The JS files are downloaded but not executed.
How do I get around this?
The above ajax call and the container element are present in a html file called info.htm.
And edit.htm(the data fetched from the ajax call) has script tags and other html data.
Thanks.
P.S: If it helps: I can see the JS files being downloaded in the firebug 'Console' tab, however, I cant see them listed in the firebug 'Script' tab.
try with jQuery.getScript("url") for more details refer this
It makes ajax call implicitly.
or try something like :
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
$(data).appendTo(container);
},
});
I had once a problem with CSS - it had incorrect Content-Type header and was not interpreted by the browser. Can that be the problem? Does your server return text/javascript for the requested script?
May be your requirement is kind of odd because server doesn't return client code.
Best practice is load js file at the start, and you can call required method after completion of ajax script.
However, eval may not be a good practise but this is one solution.
To execute javascript code which is in variable, you need to use eval method.
Please take a look at this reference http://www.w3schools.com/jsref/jsref_eval.asp
hope this helps.

Categories