如何忽略<元>标签?元>
我想在警报中显示包含重音字母(例如“é”和“à”)的法语句子。因此,我在 php 文件的元标记中使用 utf-8 字符集,以便字母“é”和“à”在 JavaScript 警报中正常显示。问题是,当获取 ajax responseText 时,我会在响应中获取元标记的代码。那么如何只获取php文件的echo结果呢?
这是 ajax 使用的 php 文件:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
define("ROOT_PATH", "../../");
include ROOT_PATH . 'config.ajax.php';
include ROOT_PATH . 'config.inc.php';
require_once RP_MODELS.'produit.class.php';
$prod_code = $_GET['prod_code'];
$db =& new DbConn() ;
$produit =& new produit($db->getInstance()) ;
$tab = $produit->lire($prod_code) ;
if ( $tab['cnt'] > 0)
$rep = "Le code produit " . $prod_code . " existe déjà !" ;
else
$rep = "0" ;
echo $rep;
?>
所以我只想 ajax responseText 返回 $rep 变量。如何实现这一目标?
I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". So I use the utf-8 charset in the meta tag of my php file so that the letters "é" and "à" are displayed normally in a javascript alert. The problem is that when getting the ajax responseText then I get among the responses the code of the meta tag. So how to get only the echo result of the php file ?
Here is the php file used by the ajax :
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
define("ROOT_PATH", "../../");
include ROOT_PATH . 'config.ajax.php';
include ROOT_PATH . 'config.inc.php';
require_once RP_MODELS.'produit.class.php';
$prod_code = $_GET['prod_code'];
$db =& new DbConn() ;
$produit =& new produit($db->getInstance()) ;
$tab = $produit->lire($prod_code) ;
if ( $tab['cnt'] > 0)
$rep = "Le code produit " . $prod_code . " existe déjà !" ;
else
$rep = "0" ;
echo $rep;
?>
So I want only the $rep variable to be returned by the ajax responseText. How to achieve that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
阅读:
http://php.net/manual/en/function.header.php
Read:
http://php.net/manual/en/function.header.php
您不需要在此脚本上添加元标记。您仅在浏览器必须呈现的页面上需要它。
You don't need to have the meta tag on this script. You only need it on pages that the browser is going to have to render.
从此文件中删除包括元标记的 html 代码并使用
Remove the html code including meta tag from this file and use
您需要检查请求是否是 Ajax 请求,尝试如下操作:
You need to check if the request is an Ajax request, try something like this:
您可以向 url 添加一个变量,通过该变量可以识别 AJAX 请求,也可以设置一个特殊的 X-Requested-With 标头(我认为它是由 JQuery 自动设置的)。在 PHP 中,您可以检查是否设置了此标头。
另外,不要忘记设置 Content-Type 标头,其中还应该指定 UTF-8。
You can either add a variable to the url by which you can recognize the AJAX request, or you can set a special X-Requested-With header (which I think it automatically set by JQuery). In PHP you can check if this header is set.
Also, don't forget to set the Content-Type header, in which you should also specify UTF-8.