【问题标题】:How to get JSON from php file using get()?如何使用 get() 从 php 文件中获取 JSON?
【发布时间】:2017-01-11 09:25:10
【问题描述】:

下面是我试图从 data.php 文件中获取 JSON 的代码。我犯了什么错误?

我在这里使用 XAMPP 来获取数据。路径是正确的。我也尝试使用绝对路径,但没有运气。

在浏览器中打开绝对路径会获取我试图在此处回显的数据数组。

//Get data, and render chart
$.get("/web1/chart/data.php", function(data){
  //Get data
  var parsedData = ParseData(data);
  //Render chart
  InitChart(parsedData);
});

【问题讨论】:

  • data.php 中有什么内容?
  • 你试过 JSON.stringify(data) 了吗?
  • 给我们看看data.php的代码?
  • 什么是ParseData()InitChart() 是什么?当你做console.log(data);时你会得到什么
  • 这是js代码。不是php代码。使用 php 使用 include_once 或 require。对于 api 使用 curl

标签: javascript php jquery sql-server json


【解决方案1】:

在javascript中,你没有提到数据类型是JSON,请尝试使用$.getJSON(而不是$.get

否则,在您的 get 请求中将数据类型指定为 JSON。

$.get("/web1/chart/data.php", function( data ) {
  alert(JSON.stringify(data));
}, "json" );

【讨论】:

  • 我尝试使用 $.getJSON 。它必须是一个 .JSON 文件。但这里它是一个 .php 文件。所以不会工作
  • 编辑了我的答案以获得更好的清晰度。尝试第二个答案怎么样。
猜你喜欢
  • 2019-04-02
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多