MIME 类型“application/xhtml+xml”的严格 XHTML:AJAX / PHP 的 DOMDocument 类失败

发布于 2024-12-16 12:17:02 字数 1441 浏览 1 评论 0原文

很久以前,我就按照这个 示例 建模了 AJAX 搜索。我改变了一些东西 - 整个请求过程现在通过 jquery 处理。但请求的PHP文件基本是一样的。我添加了一个用于多字节兼容性的标头...

header("content-type: text/html; charset=utf-8");

但除此之外:很少编辑。而且效果很好。直到最近:我想切换到严格的 XHTML,并且一直在转换我的 html 代码。我的意思是严格:我什至将 MIME 类型更改为“application/xhtml+xml”。唯一的问题是:当切换到这种类型时,AJAX 搜索不再起作用。问题不在于 jquery,而在于 PHP 的 DOMDocument 类。这是 PHP 文件的中心简化部分,通过 jquery 请求。

if (mb_strlen($searchTerm) > 2) {

  $xmlDoc = new DOMDocument('1.0', 'UTF-8');

  $xmlDoc->load('data.xml');

  echo 'Results:<br />';

  $x = $xmlDoc->getElementsByTagName('data');

  for($i=0; $i<($x->length); $i++) {

    $name=$x->item($i)->getElementsByTagName('name');

    if ($name->item(0)->nodeType==1) {

      $name = $name->item(0)->childNodes->item(0)->nodeValue;

      if (mb_stristr($name,$searchTerm)) echo $name .'<br />';

    }

  }

} else {

  echo 'Searchterm to short';

}

问题不在于 jquery,因为,当我在触发 AJAX 请求的输入字段中输入 2 个字母时,返回的文本是“Searchterm to short”(所以请求基本上是工作环)。现在出现了令人困惑的部分:当我输入一个应该在 XML 文件中找到的单词时,没有返回任何内容:没有错误,没有文本,什么也没有。当我键入 XML 文件中没有的单词时,至少会返回“结果:
”。看来:PHP 正在以某种方式循环遍历 XML 文件。当我将整个项目切换到内容类型“header('content-type: text/html; charset=utf-8')”时,一切都再次正常工作。我是否也必须更改所请求的 PHP 文件的 mime 类型 - 我已经尝试了不同的变体,但没有成功。我是否必须以某种方式更改 DOMdocument 对象才能使其在严格的 XHTML 环境中工作......?

I have modeled an AJAX search after this example a long time ago. I have changed some things - the whole request process is handled now via jquery. But the PHP file which is requested is basically the same. I have added an header for multibyte compatibilty...

header("content-type: text/html; charset=utf-8");

but other than that: little edits. And it worked quite well. Until recently: I wanted to switch to strict XHTML and have been transforming my html code. And I mean strict: I have even changed the MIME type to "application/xhtml+xml". The only problem: When switching to this type, the AJAX search isn't working anymore. The problem is not jquery, it is PHP's DOMDocument class. Here is the central simplified portion of the PHP file, which is requested via jquery

if (mb_strlen($searchTerm) > 2) {

  $xmlDoc = new DOMDocument('1.0', 'UTF-8');

  $xmlDoc->load('data.xml');

  echo 'Results:<br />';

  $x = $xmlDoc->getElementsByTagName('data');

  for($i=0; $i<($x->length); $i++) {

    $name=$x->item($i)->getElementsByTagName('name');

    if ($name->item(0)->nodeType==1) {

      $name = $name->item(0)->childNodes->item(0)->nodeValue;

      if (mb_stristr($name,$searchTerm)) echo $name .'<br />';

    }

  }

} else {

  echo 'Searchterm to short';

}

The problem lies not with jquery, because, when I input 2 letters into the input field which triggers the AJAX request, the returned text is 'Searchterm to short' (so request is basically wokring). Now comes the confusing part: When I type a word, which should be found in the XML file, nothing is returned: no error, no text, no nothing. When I type a word which isn't in the XML file, at least 'Results:
' is returned. So it seems: PHP is looping through the XML file somehow. When I switch with the whole project to the content type "header('content-type: text/html; charset=utf-8')" everything is working fine again. Do I have to change the mime type of the requested PHP file, too - I have already tried different variations without success. Do I have to change the DOMdocument object somehow to get it working in a strict XHTML enviroment...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2024-12-23 12:17:02

我很尴尬。 AJAX 请求输出违反了严格的 XHTML 规则。只是没见过而已。上面的示例运行不会出现问题,因为我已经删除了一些有缺陷的 HTML 代码。然而:问题解决了,自己的愚蠢被证实了......

I'm quite embarrassed. The AJAX request output breached the strict XHTML rules. Just haven't seen it. The example above would run without problems, because I've stripped some of the flawed HTML code. However: problem solved, own stupidity confirmed...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文