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

4

1 回答 1

2
  1. 您不能使用数字作为标识符来访问1您必须拒绝[1]的属性.1
  2. 您必须使用包含数据的变量,x而不是在您尝试将其分配到某处之前未提及的变量
  3. Ajax 中的 A 代表异步。您必须在回调getJson中处理您的数据,因为在 HTTP 响应到达之前不会调用您传递给的函数,但是document.get一旦发送 HTTP 请求,开始的行就会运行。
于 2013-08-14T15:45:16.343 回答