1

I've built a php API that provides data in json output, I need to get the values via a get request to then plot as a graph on the page.

The front end web component in hosted on the same server as in the api in this basic structure:

  • index.php
  • graph.php
  • /api/
  • /api/src
  • /api/src/api.php

My current code in graph.php is as follows:

<script>
        var myJson;

        $.getJson('api/src/api.php/poll/results/current/13/', function(jd){
          myJson = jd.AnswerCount.1;
        });

        document.getElementById('jsonhere').innerHTML = myJson; //just to test
</script>

The endpoint outputs data like the following:

{"AnswerCount":{"1":5,"3":1,"2":2,"4":1,"5":5,"6":3,"7":2}}

Which I need loaded into a key-value pair array,

1:5
3:1
2:2
4:1
...

to then be put into the graphing library.

How do I fix my code/write new code to do this? I'm pretty stuck here.

EDIT:

On a hunch I logged all the get requests via wireshark, and no request is ever sent to the url in question. Even with an empty function { } ? http://grab.kfouwels.com/pmgW

1 Answer 1

2
  1. You can't use a number as an identifier, to access the 1 property you have to say [1] not .1
  2. You have to use the variable containing your data, not x which hasn't been mentioned until you try to assign it somewhere
  3. The A in Ajax stands for Asynchronous. You have to work with your data inside your callback since the function you pass to getJson won't be called until the HTTP response arrived but the line starting document.get will run as soon as the HTTP request has been sent.
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the above, and still nothing. Looking at the packet transmissions via wireshark, no get request is ever sent to the GET url in question? Even with an empty function { } in the .getJson grab.kfouwels.com/pmgW
None at all, which is the odd thing

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.