How to code one jquery function for all AJAX links

前端 未结 4 963
生来不讨喜
生来不讨喜 2020-12-11 08:25

I am using zend framework on windows. I want to implement ajax in my project first time. I searched for help and created a very simple ajax functionality.

In

4条回答
  •  醉话见心
    2020-12-11 09:13

    Maybe this:

    function loadData(url, callback) {
        jQuery.ajax({url: url, success: callback});
    }
    
    loadData('/index/one', function( data ) {
        jQuery('#one').html(data);
    })
    
    loadData('/index/two', function( data ) {
        jQuery('#two').html(data);
    })
    

    To make this even more compact you could define the same callback for both and then have the handler decide which element the response data should be written to.

提交回复
热议问题