Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery getJSON not working within a function
    primarykey
    data
    text
    <p>I'm having a problem with jQuery when using it within a selfdefined function.</p> <p>When I'm starting my jQuery right from my .html like:</p> <pre><code>&lt;script type="text/javascript" src="js/MyJS.js"&gt;&lt;/script&gt; </code></pre> <p>MyJS:</p> <pre><code>$.getJSON('MyFilePath', function(data) { var items = []; $.each(data, function(key, val) { //Doing things with my data. }); }); </code></pre> <p>it works fine and returns me my file. (I'm using plain text file with a json structure).</p> <p>But when I'm trying to start it from a function e.g.:</p> <pre><code>function getAllDepts() { $.getJSON('MyFilePath', function(data) { var items = []; $.each(data, function(key, val) { //Doing things with my data. }); }); } </code></pre> <p>it won't work. It seems like he's not able to load my file but I just don't get why.</p> <p>With:</p> <pre><code>$.ajax({ url: "MyFilePath", success: function(data){ console.log(data); }, error: function(data){ alert(error); } }); </code></pre> <p>I'd be still able to get my data but I just want to know why getJSON doesn't work. I read that getJSON has its problems with loading local files but I'm not sure if it applies to my problem.</p> <p>Any ideas?</p> <p>@comments: </p> <ul> <li>Yes I'm calling the function through an onclick:"getDepts()" in my .hmtl and it gets called correctly.</li> <li>The return value is one step ahead because the getJSON is not working correctly. It's not a problem mit jQuery thought, since when debugging with Firebug(Firefox) the method gets called. The main problem seems to be he's not able to load the file.</li> <li>My data file is an .txt. with json structure. (I checked my json structure with <a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a> and it said its okay) </li> <li>There's no error like 404 since the files are stored locally and no errors show up while using firebug.</li> <li>I created the syntax error while editing this post. Fixed the braces.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload