JQuery 错误提示:HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy

本文介绍了一个使用JQuery AJAX在不同浏览器间遇到的问题及其解决方案。问题表现为在火狐等浏览器中无法正确插入和执行返回的HTML和脚本代码。通过设置dataType属性为html,实现了跨浏览器兼容性。

用JQuery的AJAX返回一些script脚本和html代码并运行这些代码,在测试的时候,发现在IE下正常,在火狐下并没有数据出现,通过调试,发现提示'Nodecannot be inserted at the specified point in thehierarchy'错误,百思不得其解,谷歌和百度均没有有效的解决办法。


通过查看JQuery的API。最终发现了问题所在。

下面是代码片段一,这段代码在IE下可以正常运行,但是在火狐下却提示脚本错误:

function getReservationData(d, f,url){
	jQuery.ajax({
		type: "post",
		url: url,
		data: {
			searchdata: d,
			flashcontainer: f,
			a: Math.random()
		},
		error: function (){
			return false;
		},
		success: function (data){
			$("#" +f).html(data);
		}
	});
}


这段代码是通过URL返回一个javascript脚本段,并执行这个脚本。最终在IE下测试正常,但是在火狐和谷歌浏览器下发现无法执行脚本。但是在JQuery的API中看到有一个属性dataType,通过说明,明白需要指定dataType属性为html即可解决这个问题。

最终测试正确代码如下:

function getReservationData(d, f,url){
	jQuery.ajax({
		type: "post",
		url: url,
		data: {
			searchdata: d,
			flashcontainer: f,
			a: Math.random()
		},
		dataType: 'html',
		error: function (){
			return false;
		},
		success: function (data){
			$("#" +f).html(data);
		}
	});
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值