如何在jquery选项卡中发送ajax请求

时间:2014-09-05 01:58:02

标签: jquery jquery-ui

这是函数

  jQuery(function($){

    $('#tabs').tabs({

      beforeLoad: function( event, ui ) {
        console.log(ui.ajaxSettings);

        if(ui.ajaxSettings.url == 'productstab/ajax/index'){
          ui.ajaxSettings.type = 'post';
        }
        ui.ajaxSettings.dataFilter = function(data, type){

        var html = data,
                   obj;
        try {
            obj = jQuery.parseJSON(data);
            html = obj.test_element;
        } catch (e) {}
            return html;
        }
      },

    })

  });

我试过

ui.ajaxSettings.username = "foo";
ui.ajaxSettings.password = "bar";

  ajaxOptions: {
      username: "foo",
      password: "bar"
  },    

但我仍然无法发送请求。我怎么能这样做?

------- -------更新

      ui.ajaxSettings.url += "?username=tester";
      ui.ajaxSettings.type = 'get';

重写到此获取使用获取发送请求 但我想使用帖子,任何想法?

1 个答案:

答案 0 :(得分:0)

这是修复

      ui.ajaxSettings.type = 'POST'; 
      ui.ajaxSettings.hasContent = true;
      ui.jqXHR.setRequestHeader( "Content-Type", ui.ajaxSettings.contentType );
      ui.ajaxSettings.data = jQuery.param( { username: 'tester'},  ui.ajaxSettings.traditional );