Cannot read property 'defaultView' of undefined 报错解决

  当我们在一个事件方法中去使用$.ajax进行数据交互时

	$('.btn_box').click(function() {
		$.ajax({
			type: 'post',
			url: '',
			dataType: 'json',
			contentType: 'application/json; charset=utf-8',
			async: false,
			success: function(res) {
				console.log($(this))
				
			}
		})
	})            

  

  此时在console 会报如标题一样的错误,

    1.因为 $ .ajax()中的$ (this)已经不是我之前所处理的了,在$ .ajax()中它指的是 ajax jQuery对象本身;


  解决办法:  

	$('.btn_box').click(function() {
		$.ajax({
                        context: this,
			type: 'post',
			url: '',
			dataType: 'json',
			contentType: 'application/json; charset=utf-8',
			async: false,
			success: function(res) {
				console.log($(this))
				
			}
		})
	})      

  只需要在ajax中加一个 context:this  锁定当前元素即可  问题解决!!!

    

猜你喜欢

转载自www.cnblogs.com/luke-fan/p/12003864.html