duanpei4455 2016-01-30 05:44
浏览 16

AJAX:这是不好的做法吗?

I've been using Ajax for a little over a month, and I've been converting a lot of my documents to use JQuery/Ajax. I've found a lot of the code is redundant, so I've been copy/pasting from one doc to another. Tonight though, after copy/pasting some Ajax code, I forgot to change the url to the reflect the doc i pasted to. But when I ran it, the code worked, using the ajax's data to call the PHP from the other document. While this worked well and certainly cuts down on file size and a lot of copy/pasting, I need to know if this is bad practice?

For example, my working document is wsparticipation.php:

function loadgroups() {
    $.ajax({
        url: 'wsparticipation.php',
        type: 'POST',
        async: false,
        data: { 'setgroups': 1 },
        success: function(re) {
            $('#groupid').html(re);
        }
    });
}

And a function just below it, one I pasted from wsmonthlyreport.php, uses the url : wsmonthlyreport.php. And I didn't copy the getmonth or getyear "procedures" to wsparticipation.php, yet it works as if I did.

function loadmonthyear(){
    $.ajax({
        url: 'wsmonthlyreport.php',
        type: 'POST',
        async: false,
        data: { 'getmonth': 1 },
        success:function(re) {
            $('#showmonth').val(re);

            $.ajax({
                url: 'wsmonthlyreport.php',
                type: 'POST',
                async: false,
                data: { 'getyear': 1 },
                success: function(re){
                    $('#showyear').val(re);
                }
            });
        }
    });
}

So is this good or bad practice, calling procedures from another url instead of the current url?

  • 写回答

2条回答 默认 最新

  • douwen0647 2016-01-30 08:32
    关注

    I could not say that :

    async : false
    

    is bad convention for coding, if its really bad then why jquery develop that one. Use of async:false did not allow browser to run code before ajax completion which are written just below ajax code; browser halts the execution for the completion of ajax call which we call the synchronous call.

    Suppose, we need initialize JQuery UI component and data must be loaded from ajax, UI component may be initialized with out data, to ensure such then async:false is only the way to guarantee, otherwise for the general use you should use async:true;

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)