码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-08 01:31:32      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:property   type   btn   class   数据   font   UNC   contex   处理   

  当我们在一个事件方法中去使用$.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  锁定当前元素即可  问题解决!!!

    

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

标签:property   type   btn   class   数据   font   UNC   contex   处理   

原文地址:https://www.cnblogs.com/luke-fan/p/12003864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!