My "JSON" isn't working - javascript

I'm loading a large number of items on a page through JSON, but it isn't working. This is my first time using JSON and I figured JSON was just a big object so I copied my object that I had before in a variable into a file and names it fun.js.
You can check out the JSON here:
http://justpaste.it/15zc
I'm using jQuery to get the JSON:
$.getJSON('fun.js', function(data){
alert(data)
});
Nothing is being alert, in the matter of fact...the alert isn't happening at all. Anyone know why?

For starters, your JSON doesnt validate. Go paste it here and fix your errors: http://jsonformatter.curiousconcept.com/
Secondly, user jQuery.Ajax to which you can pass onError parameter so you'll get a warning that JSON didn't go through.

Have you validated that the JSON is the issue? Open up Firebug or Chrome Dev Tools and refresh the page. You may see a message that the file wasn't found or there was a parsing exception or a security error.
If the file isn't found, you can fix that in the code.
If there is a parsing error, use a JSON validator.
If there is a security issue, see my answer to Get a JSON file from URL and display. You need to update your server policy or configure your browser to allow access to file URL's in your tests through.

Related

My php script return a 403 error whene i call him

I have a little problem with PHP:
On my site, I call the PHP script with the fetch method in javascript to dynamically record log files or to list the files in a folder with the PHP blob() function.
My problem is that locally everything works fine, but once on the server, I get the following error with a 403 code:
Forbidden
You don't have permission to access this resource.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
I tried to put the rules to 777 on the files, nothing to change. I also tried to delete the .htaccess (in case a rule disturbs), but no result.
I dry a bit, and I don't know what to try :/
If you have an idea, I would like to know her!
Thanks for your time :)
EDIT :
After a lot of tests, I discovered that the same code works perfectly on another server... so it's probably the configuration of the server I need to change, but what? I don't know exactly, ^^.

GitHub network/meta and network/chunk Error 400 - Bad Request

I'm trying to get the meta and chunk data of a repository network in GitHub.
I looked inside the source code of the network view of a repository and apparently you can get the meta and chunk data via:
"/user/repo/network/meta"
and
"/user/repo/network/chunk&nethash=NETHASH_HERE".
By debugging the page I found out that the needed nethash for the chunk is inside the meta data. But everytime I enter the URL I get a "Bad Request" even though I copy paste it directly from the debugged network view of a repository (and there it apparently works..).
I also tried using the nethash I got from debugging the page but still a bad request.
As an example I tried this link. It's the request for the chunk data with the nethash parameter.
Does anyone have an idea on what I'm doing wrong?

How to use PowerShell output in JavaScript

I would like my JavaScript to use the PowerShell output, which is in JSON format, so I can draw a Google data table.
I have tried using window.clipboardData.getData('Text') but it doesn't seem like the answer I'm looking for and for some reason setTimeout() isn't working anymore. I have also tried event.dataTransfer.getData('Text') but I received an error
SCRIPT5007: Unable to get property 'getData' of undefined or null reference.
I kind of don't like having to copy to the clipboard because PowerShell does not take the same amount of time to finish executing every time. Additionally, the Google portion of the code constantly responds with error Invalid JSON string even though when I manually pasted the code, the data table loaded perfectly. I'm running on IE11.
You can install a HTTP server (like Apache or Lighttpd) on your computer and dump your JSON output in a .json file in the HTTP server directory.
Then you can make an ajax HTTP request to retrieved the file everytime you need it.

Error handling when generating pdf or other non-html file

In our web portal we generate PDFs for certain kinds of data. The user downloads the PDF by clicking an tag that references something that we return with content-type: application/pdf;charset=utf-8
This works well when it works; the browser realizes that it is getting a PDF file and opens a internal or external PDF reader, or asks the user to save the file, depending on browser and user configuration.
We have some cases where we may fail to generate the PDF though. First we didn't handle the error, a NullPointerException fell through and we got an ugly new page full of JSON formatted garbage. Then we tried returning an empty result, which the browser thinks is fine and just saves or sends an empty file. Then I tried returning a redirect, which confused Chrome which showed an alert telling the user that something strange was happening.
The href in the tag is on the format "/module/showmypdf.cmd?pdfid=67482". This, as I said, works fine when a valid pdf is returned.
So, is there any kind of best practice for error handling when it comes to sending non-HTML files to browsers? Is there something else I could try to make the browser interpret my response as a redirect?
Ok I figured out why the redirect didn't work. I was doing this in my Java Spring controller:
response.sendRedirect("redirect:mypage.html?pdfError=true");
The "redirect:" prefix is something you can use when returning the view name from a controller. In the sendDirect() call it only adds confusion. Removing "redirect:" fixed it.

Browser Problems Parsing XML With JQuery

I need to access XML files with JQuery. I tried these two ways but I can't seem to contain them on one browser.
Here:
In Chrome, this works:
var xml = "<music><album>Beethoven</album></music>";
var result = $(xml).find("album").text();
alert(result);
Now I try to use it in conjunction with this code snippet, which works fine in IE:
jQuery.get('fruits.xml', function(data) {
alert(data);
});
The problem is, if I put both these codes together, one of them don't work on the other. So in Chrome I'd be able to access "Beethoven" while not being able to access "fruits.xml" but gives me this error:
XMLHttpRequest cannot load file:///C:/Python32/fruits.xml. Origin null is not allowed by Access-Control-Allow-Origin.
In IE on the other hand, I could access the whole content of fruits.xml and save it into a variable but the code for which I need to access XML attributes which works in Chrome doesn't work in IE.
As you can see, I want to get the contents of the xml using the 2nd snippet of code, while I will access the contents of the xml using the 1st snippet of code. Is there another way to access XML with Javascript? Can you help me with what's wrong with my codes?
Help?
The problem causing the error message is that you're sending an XHRRequest (The A in *A*JAX) to a file:// URL. For security reasons, this is being disabled by modern browsers. Instead, set up a webserver und it to access your page, via http://localhost/.. instead of file://C:/....
For the second part, make sure that you test loading fruits.xml first. Chances are there is an error in the XML stored in this file, or its structure is not what you expect. You can debug JavaScript in IE by pressing F12, going to the Scripting tab, and clicking Start Debugger. That way, you'll get a better error description than "doesn't work".
this may be a bug associated with chrome
loading local files in chrome bug/security feature
Problems with jQuery getJSON using local files in Chrome
Accessing relative URL's via "ajax" from "file://" content
as an advice dont wrap your xml in jquery and parse this way it may vary from browser to browser use instead .parseXML

Categories

Resources