2

I'm having a problem with jQuery when using it within a selfdefined function.

When I'm starting my jQuery right from my .html like:

<script type="text/javascript" src="js/MyJS.js"></script>

MyJS:

$.getJSON('MyFilePath', function(data)
{
  var items = [];
  $.each(data, function(key, val)
  {
  //Doing things with my data.
  });
 });

it works fine and returns me my file. (I'm using plain text file with a json structure).

But when I'm trying to start it from a function e.g.:

function getAllDepts()
{
  $.getJSON('MyFilePath', function(data)
  {
    var items = [];
    $.each(data, function(key, val)
    {
    //Doing things with my data.
    });
  });
}

it won't work. It seems like he's not able to load my file but I just don't get why.

With:

$.ajax({
             url: "MyFilePath",
             success: function(data){
             console.log(data);
             },
             error: function(data){
             alert(error);
             }
             });

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.

Any ideas?

@comments:

  • Yes I'm calling the function through an onclick:"getDepts()" in my .hmtl and it gets called correctly.
  • 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.
  • My data file is an .txt. with json structure. (I checked my json structure with http://jsonlint.com/ and it said its okay)
  • There's no error like 404 since the files are stored locally and no errors show up while using firebug.
  • I created the syntax error while editing this post. Fixed the braces.
7
  • Look at your console and see if you have any issues such as 404. Commented Apr 3, 2013 at 13:11
  • 1
    Are you calling the getAllDepts function? Commented Apr 3, 2013 at 13:11
  • 1
    Do you actually call the function? Commented Apr 3, 2013 at 13:12
  • 2
    How is getAllDepts() called? Is your file .json or is it .txt? Do you see errors in the console? Commented Apr 3, 2013 at 13:18
  • 3
    It doesn't work because you have a mess with braces in your function definition. Syntax error. Commented Apr 3, 2013 at 13:24

2 Answers 2

1

Your function doesnt look right try

function getAllDepts() {
    $.getJSON('MyFilePath', function(data) {
        var items = [];
        $.each(data, function(key, val) {
            //Doing things with my data.
        });
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you're right but it was only an error while creating this post here. In my programm I had the missing ");".
0

Try the following. This may have something to do with the asynchronous nature of $.getJSON.

(document).ready(function() {

    function getAllDepts()
    {
      $.getJSON('MyFilePath', function(data)
      {
        var items = [];
        $.each(data, function(key, val)
        {
        //Doing things with my data.
        });
      });
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.