
2007-6-1 21:24
sxpo
请教一个ajax的简单问题
为什么responseText里面已经是字符串 yes
都alert 出来了
可是 xmlHttp.responseText == "yes" 这个表达式却返回false呢?
代码如下:[code]html & javascript
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language = 'javascript'>
//创建XMLHttpRequest对象实例,反正都差不多,省略
//startRequest函数,
function startRequest(username)
{
createXMLHttpRequest();
xmlHttp.open("GET","ckuser.php?name="+username,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
//在这里alert xmlhttp.responseText 出来 已经取出 yes or no
//但是xmlHttp.resposeText好像既不等于字符串yes 也不等于字符串no
//这两个if 语句都直接跳过了
if (xmlHttp.responseText=="yes")
{
document.getElementById("ckuser").innerHTML = '被注册';
} else if (xmlHttp.responseText=="no")
{
document.getElementById("ckuser").innerHTML = '未注册';
}
}
}
}
</script>
//省略部分HTML代码
<input type="text" name="username" value="" />
//省略部分HTML代码
php函数
<?php
$username = $_GET["name"];
$link = mysql_connect('localhost:3306', 'root', '123456')
or die('Could not connect: ' . mysql_error());
mysql_select_db('star') or die('Could not select database');
$query="select id from users where username='".$username."';";
$res=mysql_query($query) or die('Query failed: ' . mysql_error());
if(mysql_num_rows($res)!=0)
{
echo "yes";
}else
{
print "no";
}
?> [/code]
[[i] 本帖最后由 sxpo 于 2007-6-1 21:36 编辑 [/i]]
2007-6-1 21:35
sxpo
如果 php 中把下面那段换成 如下:
[code]
if(mysql_num_rows($res)!=0)
{
echo 1;
}else
{
print 0;
}
[/code]
把javascript中同等代码换成下面这样
[code]
if (xmlHttp.responseText==1)
{
document.getElementById("ckuser").innerHTML = '被注册';
} else if (xmlHttp.responseText==0)
{
document.getElementById("ckuser").innerHTML = '未注册';
}
[/code]
这样的话就能正确的判断出responseText的返回值了 ,不明白为什么
不是responseText返回的是字符串么? 也就是返回值应该为"1"才对,那xmlHttp.responseText 就应该不等于数字1了啊?
总结一下:
当xmlHttp.responseText的返回值是一个字符串"1"的时候
xmlHttp.responseText == '1'
xmlHttp.responseText == "1"
xmlHttp.responseText == "0"
xmlHttp.responseText == '0'
上面这些表达式返回的值都是false
只有 xmlHttp.responseText == 1 这个表达式返回的值才是 true 为什么会这样啊?
2007-6-1 21:49
cid73
你可以 alert(request.responseText.length) 看看, 也许是因为输出了一些多余的空格所导致.
另外对这种情况你不如送 header, 如 header("MyHeader: " . true or false), 在客户端用 if ( request.getResponseHeader("MyHeader") ) 来判断.
2007-6-1 21:53
cid73
如果用 if (xmlHttp.responseText === 1) 情况会不同的, 而且 XMLHttpRequest 要求服务器端内容编码必须为 Unicode.
2007-6-1 22:43
sxpo
[quote]原帖由 [i]cid73[/i] 于 2007-6-1 21:49 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=180&ptid=44][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
你可以 alert(request.responseText.length) 看看, 也许是因为输出了一些多余的空格所导致.
另外对这种情况你不如送 header, 如 header("MyHeader: " . true or false), 在客户端用 if ( request.getResponseHeader ... [/quote]
感谢cid73老大!
说的真不错!
就是多了些东西
no 明明是两个字符,显示有5个长度,换行了 多了个/n 但也不是三个啊
我再研究研究
2007-6-1 22:52
sxpo
[code]
var lin = xmlHttp.responseText.Trim();
if (lin=="yes")
{
document.getElementById("ckuser").innerHTML = '被注册';
} else if (lin=="no")
{
document.getElementById("ckuser").innerHTML = '未注册';
}
[/code]
确实是...我把多余的字符去掉就可以了,下一步再看看这多余的字符怎么产生的
2007-6-5 18:17
sxpo
BOM :Q
2007-6-6 00:59
Haohappy
呵呵,注意编码统一,用UTF-8一定要注意。
2007-6-6 20:17
cid73
实际上用整个文档向客户端传送一两个变量我个人觉得很不明智, 容易出错是一回事, 还有当你要传送的是一组很简单的变量你就不得不用 JSON 或 XML, 这样简单的问题就变复杂了.
因此我总是利用 header 来传送简单变量, 比如在服务器端:
...
$controller->sendRequestSuccessHeader(); // header("Request-Success: 1");
or
$controller->sendRequestFailureHeader(); // header("Request-Failure: 1");
...
在客户端:
...
if ( Controller.getRequestSuccessHeader(request) ) // return request.getResponseHeader("Request-Success")
...
2007-6-6 21:27
sxpo
[quote]原帖由 [i]Haohappy[/i] 于 2007-6-6 00:59 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=202&ptid=44][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
呵呵,注意编码统一,用UTF-8一定要注意。 [/quote]
一开始的时候我就把zde editplus dw编码全改成UTF-8了
但中间那个文档好象是用记事本打开过一次 中间就出了这个问题
但我到现在还是不知道那多出来的三个字符是怎么产生的,应该不是BOM
以后转过来再看
2007-6-6 23:22
sxpo
[quote]原帖由 [i]cid73[/i] 于 2007-6-6 20:17 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=204&ptid=44][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
实际上用整个文档向客户端传送一两个变量我个人觉得很不明智, 容易出错是一回事, 还有当你要传送的是一组很简单的变量你就不得不用 JSON 或 XML, 这样简单的问题就变复杂了.
因此我总是利用 header 来传送简单变量, ... [/quote]
cid73老大 对不起
我试了半天
header()里面的参数我用javascript接收不到啊,你能再罗嗦一点么
2007-6-7 01:56
cid73
一个简单示例, 实用中你应该尽量使用一个更好的抽象层如 prototype.js
[code]
<?php
define('MY_HEADER_NAME', 'Header-Contents');
if ( !empty($_GET['headerContents']) ) {
header(MY_HEADER_NAME . ': ' . $_GET['headerContents']);
exit;
}
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> New Document </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
var msxmls = new Array(
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP');
for (var i = 0; i < msxmls.length; i++) {
try {
return new ActiveXObject(msxmls[i]);
} catch (e) {
}
}
return null;
};
}
function getHeader(contents) {
var url = '?headerContents=' + contents;
var request = new XMLHttpRequest;
request.onreadystatechange = function() {
if ( request.readyState == 4 && request.status == 200 ) {
alert(request.getResponseHeader('<?php echo MY_HEADER_NAME ?>'));
}
}
request.open('GET', url);
request.send(null);
}
</script>
</head>
<body>
<button onclick="getHeader('Tom')">Get Tom's Name</button>
<br />
<br />
<button onclick="getHeader('Alice')">Get Alice's Name</button>
<br />
<br />
<button onclick="getHeader('Jimi')">Get Jimi's Name</button>
</body>
</html>
[/code]
你可以用一些工具如 [url=https://addons.mozilla.org/en-US/firefox/addon/3829]Live HTTP Headers[/url] 来观察每一次请求所获得的 header 内容.
2007-6-12 00:03
sxpo
[quote]原帖由 [i]cid73[/i] 于 2007-6-7 01:56 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=207&ptid=44][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
一个简单示例, 实用中你应该尽量使用一个更好的抽象层如 prototype.js
New Document
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
var msxmls = ... [/quote]
感谢cid73老大,我看了好几天prototype.js 虽然还有很多不明白的地方,但也已经学到很多了,慢慢学
确实是个好东西,感谢您的推荐
页:
[1]
Powered by Discuz! Archiver 5.5.0
© 2001-2006 Comsenz Inc.