我有一个代码可以从php文件中获取数据。 这是:
function getMods(){
var init;
$.ajax({
type: "POST",
url: "init.php",
data: { 'id': getID()},
cache: false,
success: function(data)
{
init = data;
},
async:false
});
return init;
}
这是php文件:
<?php
include('dbconnect.php');
if(isset($_POST['id'])){
$id = $_POST['id'];
$value = "";
$init = array("Name","Owner","Admin1","Admin2","Admin3","Admin4");
for($i = 0; $i > 6;$i++){
$value[$i] = chatMods($init[$i],$id,$username,$password);
}
echo json_encode($value);
}
?>
Php文件发送数据是一个字符串。我想发送一个字符串数组的数据。我怎么能这样做?
答案 0 :(得分:1)
如果要从PHP发送数组,最好使用json:
在您的PHP文件中:
$data = array('hello', 'world');
echo json_encode($data);
JS:
$.ajax({
type: "POST",
dataType: "json",
url: "init.php",
data: { 'id': getID(), 'Name': name },
cache: false,
success: function(data)
{
console.log(data);
},
async:false
});