duangelin7513 2012-01-21 15:37
浏览 43
已采纳

客户端页面呈现,好坏(PHP + Javascript \ AJAX)

I am designing the back-end for a web application. I am in a situation where I am unable to collaborate with the front end developer. I also do not like inline HTML\PHP and am not in a position to integrate a template. My solution is that I would keep the front end completely separate from the back-end. The front end would communicate with the back end via AJAX (or jQuery). For example the file upload system would work like this:

        <?

            //Do stuff
            if(!isUserLoggedIn())
                die("0-Must be logged in");

            //Do more
            if(!fileIsTooBigOrNotTheRightType()) {
                die("0-Bad files");
            }

            //Do even more
            if(!move_uploaded_file($src,$dst) {
                die("0-directory error");
            }

            echo "1-Success";

        ?>

The front end developer will simply AJAX the file to upload.php and interpret the response and take the appropriate action on the front end (Show the form, red out some fields, display an error message, redirect to the login form, etc.)

To reduce the number of HTTP queries each page may contain a hidden field with JSON data containing the dynamic elements that will be in place. For example on a private messaging system's inbox page the JSON string may include things such as the number of new messages, who each message is from, and the subject. The front end developer will be able to parse that data and use it any way he likes and is free to create a user friendly interface independent of me.

It is 2012 and I don't think it is wrong to assume the client's have javascript with AJAX support.

TLDR: I am shifting the work of rendering the page (putting elements in their place) to client side javascript. From an efficiency standpoint is this good or bad? It seems like less work the server has to do.

  • 写回答

3条回答 默认 最新

  • doudiandi6967 2012-01-21 15:43
    关注

    Being both a front and back end developer, my general approach is server-side first, then client side. Make the application work completely and thoroughly without any JavaScript, then move on (or let the front end guy) to the client-side. This ensures that everyone can use your app, even those who don't use JavaScript, such as screen readers and search engines.


    What you're referring to is called an API. You make the interface, and then have the frond end developer make API calls to you.

    It's possible, though you should be very clear about the specifics of the API (what you can do, and what you can't).

    Also make sure you're secure (so that not everyone can access your API).

    Other than that, you can do it, sure, no problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题