Php jSON在使用.getJSON时不导出我的数组
我试图使用get从一个按钮获取信息,然后吐出一个数组。到目前为止,在运行这段代码时,没有调用Php jSON在使用.getJSON时不导出我的数组,php,jquery,getjson,Php,Jquery,Getjson,我试图使用get从一个按钮获取信息,然后吐出一个数组。到目前为止,在运行这段代码时,没有调用.getJSON(当我执行查询时,它可以工作,但我找不到查询中的任何问题) 以下是相关代码: Javascript: $.getJSON("/westcoast_map.php", // The server URL { westcoast_id: $('.opener').data('westcoast_id') }, // Data you want to pass to the server
.getJSON
(当我执行查询时,它可以工作,但我找不到查询中的任何问题)
以下是相关代码:
Javascript:
$.getJSON("/westcoast_map.php", // The server URL
{
westcoast_id: $('.opener').data('westcoast_id')
}, // Data you want to pass to the server.
function (json) {
var id = json[0];
waypts = json[1];
alert(id);
console.log(waypts);
});
php脚本(westcoast_map.php):
按钮:
<button type="button" class="opener" data-westcoast_id="westcoast_id" onclick="calcRoute();">
West Coast
</button>
西海岸
在我看来,您的值缺少键
,对于状态,请尝试以下操作:
if($row['city'] != '' && $row['state'] != '') {
$westcoast_array[] = "{
location:".$row['city'].",
state:".$row['state'].",
stopover:true
}";
}
您还将数组用作键,但这是无法做到的。将$westcoast\u array[$row]
更改为$westcoast\u array[]
如果我错了,我很抱歉,但我认为您正在自己制作JSON
,并使用JSON\u encode
再次对其进行编码。试试这个:
<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");
$westcoast_id = $_GET['westcoast_id'];
$westcoast_array = array();
$query = "SELECT city, state FROM users GROUP BY city";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
if ($row['city'] != '' && $row['state'] != '') {
$westcoast_array[] = array("location" => $row['city'], "state" => $row['state'], "stopover" => true);
}
}
$data = array(
$westcoast_id,
$westcoast_array
);
echo json_encode($data);
?>
我不是PHP爱好者,但在我看来,你似乎在这里做一些json格式化($westcoast_数组[$row]=“{location:“$row['city']”、“$row['state']”、stopover:true}”)——我认为你不应该这么做——json_编码应该处理它。这里的PHP爱好者……你浏览过PHP处理程序了吗?有错误吗?如果您在地址栏中手动输入$_getparams,输出结果是什么?我得到以下信息:第19行的/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/westcoast_map.php中的非法偏移量类型
并且我正在尝试导出json数组ha中的数组(我希望json[1]是创建的数组)。第19行是$westcoast_数组[$row]=“{location:.$row['city'].“,“$row['state'.]”,stopover:true}”行看起来您缺少state值的键,请尝试:{location:“.$row['city'.]”,state:“.$row['state'.]”,stopover:true}
<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");
$westcoast_id = $_GET['westcoast_id'];
$westcoast_array = array();
$query = "SELECT city, state FROM users GROUP BY city";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
if ($row['city'] != '' && $row['state'] != '') {
$westcoast_array[] = array("location" => $row['city'], "state" => $row['state'], "stopover" => true);
}
}
$data = array(
$westcoast_id,
$westcoast_array
);
echo json_encode($data);
?>