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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能