0

What I am trying to achieve is to graph a table using Google's graph API, when the page loads, it queries my REST server for the JSON data. This is my Javascript function.

The JavaScript console doesn't show any errors.

 $(document).ready(function() { 
        $.ajax({
                      url: 'http://localhost:8004/project5',
                      dataType: "json",
                      async:false,
                      error: function(error){
                             console.debug(error);
                         },
                      success:  function(data)
                        {
                                alert("YESSSSS");   
                                var data = data.results;    
                                for(var i=0; i<data.length; i++) {

                                    mytable[arraysize] = new terms(data[i].term1, data[i].term2, (data[i].contains)/((data[i].contains)+ (data[i].notcontains)));
                                    arraysize +=1;

                                }

                                    drawTable();

                        }


                    });
    });

This Is the JSON that is returned when I query the server in my browser using "http://localhost:8004/project5"

{
  "results":[
  {
     "term1":"test",
     "term2":"hard",
     "contains":"32",
     "notcontains":"55"
  },
  {
     "term1":"test",
     "term2":"easy",
     "contains":"32",
     "notcontains":"55"
  },
  {
     "term1":"pizza",
     "term2":"hut",
     "contains":"32",
     "notcontains":"55"
  }
   ]
}

The html contents of content isn't set to "YESSSS" which i was using to test to see if the function was a success. When the page loads, the rest server recognizes that it is queried and successfully returns the JSON, so I believe the problem is getting the data out of the returned JSON. Which is where I am stuck.

EDIT: it seems that my success function isn't being called. This is the function that is called in the java REST server, JAX-RS. To produce the JSON.

 @GET
  @Produces(MediaType.APPLICATION_JSON)
  public String getStudentByid(@QueryParam("id") String id) {

      System.out.println("Queried");
      if (id == null)
          return Terms.stringTerms(container);
      return Terms.getTermsByFirst(container, id);
 }

I am getting a XMLHttpRequest cannot load http://localhost:8004/project5/terms.json. Origin null is not allowed by Access-Control-Allow-Origin. error now

9
  • Does the console show any errors? Commented Nov 12, 2012 at 6:53
  • The console doesn't show any errors. Commented Nov 12, 2012 at 6:55
  • The content element has a class of "content" and not an ID, correct? I ask because the ID situation seems more likely. Commented Nov 12, 2012 at 6:59
  • Usually when debugging ajax stuff I add something like this line to the success function window.ajax_result = data. Then you can play with the result in the console. Commented Nov 12, 2012 at 7:00
  • 1
    Are you serving the HTML from the same server as the JSON? If you serve the page from localhost and the JSON from localhost:8004 you will get a cross domain error. In my experience they are sometimes not reported in the console. Commented Nov 12, 2012 at 7:19

2 Answers 2

1

Reading your last edit, it seems you have a cross site scripting problem. You are making an ajax request to a site which is in a different domain to where your HTML page was served.

Maybe you only have this problem in development, and in production the HTML page will come from the same site as the ajax request. Then you should look at making your development environment similar to a real deployment.

For example, using a 'real' web server to deliver your HTML page, and not just opening the HTML page from a local drive.

Sign up to request clarification or add additional context in comments.

Comments

0

You should simply put console.log("step1")

or 2 or 3, to various parts of your code and see where it stops logging. Thats where your issue is.

You can also log anything else that might give you a clue.

3 Comments

And as suggested, liberally assign and test window.someattributes
This is not the problem, i know the success function is not working. I narrowed it down to a problem with the way the JSON is returned from the server. If I querry it with http://localhost:8004/project5/terms.json through my browser it works fine. However when the java script queries the server it doesn't receive any information.
Yeah, I get it. Maybe it is the issue with the headers of your request. I added another answer.

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.