重新构建php以读取和发送json响应
我有一个新项目,前一个开发者没有完成。我正在尝试重建action.php重新构建php以读取和发送json响应,php,json,ajax,Php,Json,Ajax,我有一个新项目,前一个开发者没有完成。我正在尝试重建action.php $.ajax({ type: "POST", url: "action.php", data: {action:'upload', image:data, //data is data:image/png;base64 uri count:uploadNum, uniqid:uniqid} }).done(function(o)
$.ajax({
type: "POST",
url: "action.php",
data: {action:'upload',
image:data, //data is data:image/png;base64 uri
count:uploadNum,
uniqid:uniqid}
}).done(function(o) {
var data = $.parseJSON(o);
if (!data || data === null) {
toggleError(true);
}else{
if(data.status==true){
uploadLoopCount++;
if(uploadLoopCount < upload_arr.length){
uploadImage();
}else{
readySave = true;
toggleShareButtons('ready');
var loc = location.href
loc = loc.substring(0, loc.lastIndexOf("/") + 1);
loc += '?id='+uniqid;
$('#shareLink').val(loc);
}
}else{
toggleError(true);
}
}
});
<?php
if ($_POST['action']=='load') {
$uid=$_POST['uniqid'];
// fetch contents from db with $uid;
header("content-type:application/json");
$data[] = array('status' =>'true','profile1_data' =>'green','profile2_data' =>'blue','profile3_data' =>'orange','profile4_data' =>'green','profile5_data' =>'red' ,'upload' =>'http://localhost/img/', 'format' =>'jpeg' );
$j = json_encode($data);
echo $j;
}
if ($_POST['action']=='upload') {
header("content-type:application/json");
//confused with reading json response
// upload the contents
$data[]= array('status' =>'true' );
$j=json_encode($data);
echo $j;
} ?>
我已经尝试创建action.php,上传功能会将图像上传到某个目录中,并且会有一些数据库链接。加载部分将从数据库返回内容。我对php的json部分感到困惑
这是raw action.php
$.ajax({
type: "POST",
url: "action.php",
data: {action:'upload',
image:data, //data is data:image/png;base64 uri
count:uploadNum,
uniqid:uniqid}
}).done(function(o) {
var data = $.parseJSON(o);
if (!data || data === null) {
toggleError(true);
}else{
if(data.status==true){
uploadLoopCount++;
if(uploadLoopCount < upload_arr.length){
uploadImage();
}else{
readySave = true;
toggleShareButtons('ready');
var loc = location.href
loc = loc.substring(0, loc.lastIndexOf("/") + 1);
loc += '?id='+uniqid;
$('#shareLink').val(loc);
}
}else{
toggleError(true);
}
}
});
<?php
if ($_POST['action']=='load') {
$uid=$_POST['uniqid'];
// fetch contents from db with $uid;
header("content-type:application/json");
$data[] = array('status' =>'true','profile1_data' =>'green','profile2_data' =>'blue','profile3_data' =>'orange','profile4_data' =>'green','profile5_data' =>'red' ,'upload' =>'http://localhost/img/', 'format' =>'jpeg' );
$j = json_encode($data);
echo $j;
}
if ($_POST['action']=='upload') {
header("content-type:application/json");
//confused with reading json response
// upload the contents
$data[]= array('status' =>'true' );
$j=json_encode($data);
echo $j;
} ?>
我对json响应和作为json的响应感到困惑。帮助我重建action.php。提前谢谢