的XmlHttpRequest [英] XmlHttpRequest
问题描述
在Internet Explorer 6中,我遇到了httprequest
对象的问题。我用它来调用web服务并在
readystate事件处理程序中显示结果。这是我第一次调用它时的工作原理,但随后从未调用readystate事件处理函数,或
事件本身永远不会被激活。
>
相同的代码在Firefox中运行良好。改变它很多没有
效果有人可以帮忙吗?
我不应该每次创建一个新的XmlHttpRequest对象
我想打电话给远程程序吗?
希望你不介意长篇文章,但这里的代码很简单
够了:
<!doctype html public" - // W3C // DTD HTML 4.0 Transitional // EN">
< html>
< head>
< title>新文件< / title>
< / head>
< script type =" text / javascript">
//启动XMLHttpRequest对象
//在此处找到: http://www.webpasties.com/xmlHttpRequest
函数getHTTPObject(){
var xmlhttp;
/ * @ cc_on
@if(@_jscript_version> = 5)
尝试{
xmlhttp = new ActiveXObject (" Msxml2.XMLHTTP");
} catch(e){
try {
xmlhttp = new ActiveXObject(" Microsoft。 XMLHTTP");
} catch(E){
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @ * /
if(!xmlhttp&& typeof XMLHttpRequest!=''undefined'')
{
try {
xmlhttp = new XMLHttpRequest();
} catch(e){
xmlh ttp = false;
}
}
alert(" instantiated");
返回xmlhttp; < br $>
}
var Request = getHTTPObject();
函数GetTreeOutput()
{
尝试
{
if(Request!= null)
{
if(Request.readyState == 4 || Request.readyState == 0)
{
var sUrl =" http://itsopt-15/webgame/Service1.asmx/HelloWorld" ;;
Request.onreadystatechange = GetTreeOutput_Response;
Request.open(" POST",sUrl,true);
Request.send(null);
}
}
其他
{
}
}
catch(e)
{
}
}
函数GetTreeOutput_Response()
{
试试
{
if(Request.readyState == 4)
{
if(Request.status == 200)
{
document.getElementById(" test")。innerHTML = Request.responseText;
}
else
{
}
}
}
catch(e)
{
}
}
< / script>
< body>
>
< input type =" button"值= QUOT;一个" onclick =" javascript :GetTreeOutput();"
/>
< input type =" button"值= QUOT;二" onclick =" javascript :GetTreeOutput();"
/>
< input type =" button" value =" Three"
onclick =" javascript :GetTreeOutput();" />
< div id =" test" style =" border:1px solid Black; background-color:#ddf;
颜色:#003;">
< / div>
< ; / body>
< / html>
In Internet Explorer 6 I''m having a problem with the httprequest
object. I use it to call a webservice and display the result in the
readystate event handler. This works the first time I call it but
subsequently the readystate event handler function is never called, or
the event itself is never fired.
The same code works fine in Firefox. Changed it around a lot to no
effect Can anyone help?
I''m not supposed to create a new XmlHttpRequest object every time I
want to call a remote procedure am I?
Hope you don''t mind the long post but here''s the code it''s simple
enough:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
</head>
<script type="text/javascript">
//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != ''undefined'')
{
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
alert("instantiated");
return xmlhttp;
}
var Request = getHTTPObject();
function GetTreeOutput()
{
try
{
if( Request != null)
{
if (Request.readyState == 4 || Request.readyState == 0)
{
var sUrl = "http://itsopt-15/webgame/Service1.asmx/HelloWorld";
Request.onreadystatechange = GetTreeOutput_Response;
Request.open("POST", sUrl, true);
Request.send(null);
}
}
else
{
}
}
catch( e)
{
}
}
function GetTreeOutput_Response()
{
try
{
if( Request.readyState == 4)
{
if( Request.status == 200)
{
document.getElementById( "test").innerHTML = Request.responseText;
}
else
{
}
}
}
catch(e)
{
}
}
</script>
<body>
<input type="button" value="One" onclick="javascript:GetTreeOutput();"
/>
<input type="button" value="two" onclick="javascript:GetTreeOutput();"
/>
<input type="button" value="Three"
onclick="javascript:GetTreeOutput();" />
<div id="test" style="border: 1px solid Black; background-color: #ddf;
color: #003;">
</div>
</body>
</html>
推荐答案
Gaz写道:
Gaz wrote:
var sUrl =" http://itsopt-15/webgame/Service1.asmx/HelloWorld" ;;
请求.onreadystatechange = GetTreeOutput_Response;
Request.open(" POST",sUrl,true);
var sUrl = "http://itsopt-15/webgame/Service1.asmx/HelloWorld";
Request.onreadystatechange = GetTreeOutput_Response;
Request.open("POST", sUrl, true);
尝试在打开后设置onreadystatechange电话改善了事情。
-
Martin Honnen
http://JavaScript.FAQTs.com/
谢谢你是问题..干杯。
Thanks that was the problem.. Cheers.
Hello Gaz,
感谢您提供有用的代码段。
虽然小小的挑剔:
Ga z写道:
Hello Gaz,
thanks for that helpful code snippet.
A small nitpicking though:
Gaz wrote:
< input type =" button"值= QUOT;一个" onclick =" javascript :GetTreeOutput();" />
<input type="button" value="One" onclick="javascript:GetTreeOutput();"/>
我想,你应该留下javascript 的:"来自onlick处理程序。这是不需要的b $ b,或者说将它放在那里是不正确的,因为我知道只允许使用
javascript代码。
干杯
Daniel Kabs
德国
I think, you should leave the "javascript:" from the onlick handler. It is
not needed or rather is is incorrect to place it there because only
javascript code is allowed, as I know.
Cheers
Daniel Kabs
Germany
这篇关于的XmlHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!