简体   繁体   中英

Parsing JSON from REST server

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. XMLHttpRequest cannot load http://localhost:8004/project5/terms.json. Origin null is not allowed by Access-Control-Allow-Origin. error now

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM