基础 - 后端控制json

    funSendHTTP({
        method: "POST",
        sHost: "http://localhost/20160728/ZhuCe.php",
        paraJSON: {
            "dtype":"json",
            "key":"保密啊",
            "menu":"鱼",
            "pn":"0",
            "rn":"100"
        },
        funSuccess: function (oXMLHttpRequest) {
            /*返回普通文本*/
            // oXMLHttpRequest.responseText;
            /**
             * 返回XML
             */
            ///*"<res><mes>...</mes></res>"*/
            // var oXML = oXMLHttpRequest.responseXML
            // oXML.getElementsByTagName("mes")[0].childNodes[0].nodeValue;
            /**
             * 返回json文本
             */
            var oJSON = JSON.parse(oXMLHttpRequest.responseText);
            console.log("成功",oJSON);
        },
        funFailure: function (oXMLHttpRequest) {
            console.log("失败");
        }
    });

  

<?php
        /*GET请求不缓存响应*/
        header("Cache-Control:no-cache");
        /*返回的字符集规范*/
        header("Content-Type:text/html;charset=utf-8");

        function funGET($str){
            return  !empty($_GET[$str]) ? $_GET[$str] : null;
        }
        function funPOST($str){
            return !empty($_POST[$str]) ? $_POST[$str] : null;
        }
        function funGetResponse($str) {
            $oCurl = curl_init();
            curl_setopt($oCurl,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($oCurl,CURLOPT_URL,$str);
            curl_setopt($oCurl,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
            $oResponse = curl_exec($oCurl);
            curl_close($oCurl);
            return $oResponse;
        }

        $dtype= funPOST("dtype");
        $key= funPOST("key");
        $menu= funPOST("menu");
        $pn= funPOST("pn")?funPOST("pn"):"0";
        $rn= funPOST("rn");
        $menuUrl = "http://apis.juhe.cn/cook/query?dtype=".$dtype."&key=".$key."&menu=".$menu."&pn=".$pn."&rn=".$rn;

        $menuResponse = funGetResponse($menuUrl);
        $menuResponseEx = json_decode($menuResponse);
        /*内容规范*/
        header("Content-type:application/json");
        echo json_encode($menuResponseEx);

?>

  

posted @ 2016-09-11 11:44  WeWeZhang  阅读(186)  评论(0编辑  收藏  举报