重新构建用于读取和发送json响应的php

时间:2015-10-26 12:55:04

标签: 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) {
    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);  
        }
    }
}); 

$.ajax({
    type: "POST",
    url: "action.php",
    data: {action:'save',
            profiledata1:JSON.stringify(userdata_arr[0]),
            profiledata2:JSON.stringify(userdata_arr[1]),
            profiledata3:JSON.stringify(userdata_arr[2]),
            profiledata4:JSON.stringify(userdata_arr[3]),
            profiledata5:JSON.stringify(userdata_arr[4])}

}).done(function(o) {
    var data = $.parseJSON(o);
    if (!data || data === null) {
        toggleError(true);
    }else{
        if(data.status==true){
            uniqid = data.uniqid;
            checkImageUpload();
        }else{
            toggleError(true);  
        }
    }
}); 

我尝试过创建action.php,上传功能会将图片上传到某个目录,并且会有一些数据库链接。并且加载部分将从数据库返回内容。我和php的json部分混淆了。

这是原始的action.php

<?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。提前致谢

0 个答案:

没有答案