403 Error using JavaScript to access folder - javascript

In a .Net Web Forms project am trying to use JavaScript to display all images in a folder. Using the following script gives me a 403 error
<script>
var dir = "fullimages/";
var filext = ".jpg";
$.ajax({
url: dir,
success: function (data) {
$(data).find("a:contains(" + filext + ")").each(function () {
var filename = this.href.replace(window.location.host, "").replace("http:///", "");
$("body").append($("<img src=" + dir + filename + "></img>"));
});
}
});
</script>
But I am not quite sure why? I have checked around the site and found the following suggestion 403 forbidden error while getting javascripts under root folder. In my case JS is not on the list of handlers.

If I have got this right your trying to run Javascript on the server side. Are you using any particular framework, node JS for example?
Secondly what server architecture are you running, windows (IIS) / linux (APACHE, N GINX) etc...?
From having a brief read of your linked question, have you tried moving this script into the fullimages directory and then request that?

Related

InDesign CS6 Socket return Empty

I have this code (InDesign CS6), and it's not working as expected. I'm using Mac OS and I need to make the code compatible with Windows and Mac.
Trying to get a text/JSON over my localhost, and the socket return an empty string:-
function getData(host, path) {
var reply = '';
var conn = new Socket;
var request = "GET " + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "\n";
if (conn.open (host)) {
conn.write (request);
reply = conn.read(999999);
var close = conn.close();
}
return reply;
}
var host = 'localhost:80';
var path = '/test/test/json.php';
var test = getData(host, path);
alert(typeof(test) + ' Length:' + test.length);
Edit: Finally I manage to find out what causing the problem. I create a VMware, and try to run the script, and it's working. Not sure why it doesn't work on my machine. Download Wireshark, and saw InDesign send the request, but something blocks the request from accessing the server. I will update if I able to detect what causing the block.
When it comes to Socket, I guess the simpliest is to take advantage of that script written by Rorohiko:
https://rorohiko.blogspot.fr/2013/01/geturlsjsx.html
Or have a try with IDExtenso library:
https://github.com/indiscripts/IdExtenso
I find those convenient as they deal with the inner socket mechanisms for you.
You do not need to use a socket just to get JSON from your server.
Instead refer to the XMLHttpRequest documentation or just a library such as jQuery which greatly simplifies making Ajax calls for JSON.

Loading SVG (XML) with YUI3

I'm trying to load a SVG file with YUI3. I've read the page about the IO Utility - YUI Library and followed the example given there. I wrote
<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>
<script type="text/javascript">
YUI().use("io", function(Y) {
Y.io('test.svg', {
on: {
success: function(id, o) {
console.log('success: ' + o.status + " ==> " + o.responseText);
},
failure: function(id, o) {
console.log('failure: ' + o.status + " ==> " + o.statusText);
}
}
});
});
</script>
The real strange thing is, that on running the script I get a "failure: 0 ==>", thus o.status is 0, which is not a HTTP status code and there is no o.statusText, although the failure event is triggered.
Here you can download the two files in a zip file.
What I'am doing wrong?
Thanks for your help!
Are you viewing the html directly in the browser? In order for Ajax to function the file needs to be on a webserver. If you are using a mac, spin up a single line webserver in the directory you are in like this:
python -m SimpleHTTPServer
Once I had your code running under a webserver it loaded the SVG properly.

Finding a Relative File Path to Parse an Excel File

I'm building a web app using Node.JS that at the very least should allow users to to upload excel spreadsheets (.xlsx) and then using an excel parser (currently using node-xlsx - https://www.npmjs.org/package/node-xlsx), I want to be able to find this file, parse it, and print its contents to the console. So far, I have the file uploaded and stored, but am having trouble specifying the file path my app should search down.
I believe my troubles are that I am trying to do this on the server-side, and I am telling my app to search through a users directory for this file when it does not have access.
Here is example code:
var fullfile;
app.post('/upload', function (request, response) {
var fstream;
request.pipe(request.busboy);
request.busboy.on('file', function (fieldname, file, filename) {
console.log('Uploading: ' + filename);
fstream = fs.createWriteStream('./storedFiles/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
response.redirect('success');
console.log('Uploaded to ' + fstream.path);
fullfile=fstream.name;
var obj = xlsx.parse(__dirname + fullfile);
console.log(obj);
});
});
});
This produces the error:
return binding.open(pathmodule._makelong(path) stringtoflags(flags) mode)
error: ENOENT, no such file or directory 'C\Users(file path on my local machine here)
Can anyone point out a way of doing this that I am missing? it has to do with fs methods I feel.
Thank you
First of all, don't use the filename that user provided when saving the file - you will get duplicates and it could be a security risk (in general, never trust user provided data). Just use your own value instead - it is more standard to use your own naming convention to prevent duplicates or to use a tmp file provided by the OS.
To solve your issue, try:
Requiring path at the top of your file:
var path = require('path');
and changing the value of fullfile to:
fullfile = path.join(__dirname,fstream.path)
then pass fullfile to xlsx.parse:
var obj = xlsx.parse(fullfile);

Implementing pdf.js in android to read from sd card

I'm trying to implement a pdf viewer for my android app which displays different pdf files which are present in the SD card.
I am thinking of using the pdf.js library.I used the code sample as posted here: https://bitbucket.org/butelo/pdfviewer/
However, the library takes the pdf url in the javascript file which is relative address to the folder to which it belongs (/assets/pdfviewer).
<script type="text/javascript">
var url = '../compressed.tracemonkey-pldi-09.pdf';
</script>
How can I redirect it to use a pdf present in a folder in the sdcard ?
Also the filenames of the pdfs are not fixed and I need to change them in the program as per requirement.
Update --------------------------------------------------------------------------------------------------------------------------
I updated the java code like this:
Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);
In the pdffile.js, I modified the following:
From:
<script type="text/javascript">
var url = '../compressed.tracemonkey-pldi-09.pdf';
</script>
To:
var url = getURLParameter('file');
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null}
The above javascript code extracts the 'file' parameter from the URL of the 'index.html'
Still does not work. The webview 'chromium' in logcat shows:
I/chromium(1353): [INFO:CONSOLE(106)] "Warning: Unhandled rejection:
Unexpected server response (0) while retrieving PDF "file:///storage/sdcard0/data/test.pdf".", source: file:///android_asset/pdfviewer/pdf.js (106)"
This seems to be cross server issue. So how can I modify the pdf.js code to read local files without server ?
Solved the issue. The app was missing READ_EXTERNAL_STORAGE permissions.
The code can be found at: https://github.com/pauldmps/Android-pdf.js
Its Apache V2 license, so feel free to use in your app.

Do we need a web server (like Apache) to access a .json file?

I was trying to read an info.json file, using the jQuery API. Please find the code below, which is part of test.html.
$.getJSON('info.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
The test.html file resides on my local machine and when I try to open it in the browser, the Ajax call is not getting triggered and the info.json file is not read.
Is it not working because I don't have a web server? Or am I doing anything wrong in the code? (I don't see any errors in the Firebug console though).
Thanks in advance.
You will always have to host your site from where you are making AJAX call. Otherwise it will throw this exception.
origin null is not allowed by access-control-allow-origin
Host your page on localhost server and I guess everything will work nicely.
While technically you don't need a web server for this, some of the libraries you use to abstract network access may not work with local files and some browsers don't let local files do a lot, so something like a little test web server for static files would be very useful for your development and testing.
Install a small webserver like http://jetty.codehaus.org/jetty/
easy to install, and small download ;)
By putting your JSON string into a text file and loading it in a iframe, you can extrapolate the data. (Most browsers can load .txt files in iframes.)
var frame = document.createElement("IFRAME"); //Create new iframe
var body = document.body;
frame.onload = function() { //Extrapolate JSON once loaded
data = JSON.parse(frame.contentDocument.documentElement.innerText); //Loads as a global.
body.removeChild(frame); //Removes the frame once no longer necessary.
}
frame.style.display = "none"; //Because the frame will have to be appended to the body.
body.appendChild(frame);
frame.src = "your JSON.txt"; //Select source after the onload function is set.

Categories

Resources