jQuery的阿贾克斯.load。获得X(高)TML相关的错误("不匹配的标签,预期:其中,/ BR>") [英] jQuery .ajax .load .get x(h)tml related error ("mismatched tag, expected: </br>")

查看:162
本文介绍了jQuery的阿贾克斯.load。获得X(高)TML相关的错误("不匹配的标签,预期:其中,/ BR>")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题其实很简单,但无论如何,我需要有人在这里深入的知识。想象一下下面这个简单的任务:加载/解析某些外部HTML数据到当前的HTML文件的一个元素

My problem is actually simple, but nevertheless I need someone with in-depth-knowledge here. Imagine the following simple task: Load / parse some external html data into an element of a current html file.

让我们说,这是外部文件的内容,我们要找回:

Let's say, this is the content of the external file, that we want to retrieve:

<div id="element">
  <img src="test.jpg"><br>
  lorem ipsum
</div>

这是使用jQuery .load的code

And this is the code using jQuery .load

$("#div_in_mother_html").load("external.html #element");

作品一样风情万种。
但看在控制台中,我看到了下面的错误

Works like charme.
But looking in the console, I see the following ERRORS:

1. mismatched tag. Expected: <br />
2. mismatched tag. Expected: <img />

这是为什么?

嗯,pretty的明显, .load 是一个缩写的阿贾克斯 jQuery中,这一点,据我了解,一个跨浏览器的< STRONG> XMLHtt prequest 。这可能会导致不匹配的标签problemss,对HTML的标签不关闭,不像XHTML:

Well, pretty obviously, .load is a shorthand for .ajax in jquery, which is, as far as I understand, a cross-browser XMLHttpRequest. This may cause mismatched tag problemss, for HTML-tags do not close, unlike XHTML:

HTML - &LT; IMG SRC =test.jpg放在&GT;&LT; BR&GT;
XHTML - &LT; IMG SRC =test.jpg放在/&GT;&LT; BR /&GT;


下一步尝试

好吧,我说,它必须能够解析不是XML,但HTML。是的,jQuery的的文档是这么说的,也是。我从.load到更先进的阿贾克斯切换,并试图手动设置数据类型:

HTML - <img src="test.jpg"><br>
XHTML - <img src="test.jpg" /><br />


Next try

Alright I say, it must be possible to parse not XML but HTML. And yes, the documentation of jQuery says so, too. I switch from .load to the more advanced .ajax and tried setting the data-type manually:

$.ajax({ type:'GET', url:'ajax.txt', contentType: "text/plain; charset=utf-8", 
 dataType:'text', success:function(data){ 
    $('#div_in_mother_html').html(data); } 
});


再次,它的工作原理......但同样沉默的错误显示在控制台中。


在本地环境的Firefox 32浏览器控制台显示的错误。


And again, it works...but the same silent errors show up in the console.


error seen in firefox 32 browser console in local environment.

推荐答案

我的猜测是,当你从文件系统工作时提出要求,这是由于某种原因,分析它的XML。你真的不应该永远从文件系统测试AJAX无论如何,因为文件系统不会返回所有相同的标题,你会得到一个真正的服务器。修复将设置一个本地网络服务器上的本地主机上运行。

My guess would be that when you request it when working from the filesystem, it is for some reason parsing it as xml. You really shouldn't ever test ajax from the filesystem anyway since the filesystem will never return all the same headers that you would get from a real server. Fix would be to setup a local webserver running on localhost.

这篇关于jQuery的阿贾克斯.load。获得X(高)TML相关的错误(&QUOT;不匹配的标签,预期:其中,/ BR&GT;&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆