Ajax不起作用 [英] the Ajax does not work

查看:94
本文介绍了Ajax不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这段代码是如此简单,但是它不起作用,我也不知道为什么?

I try this code it is so simple, but it does not work and I don''t know why?

<html>
<head>

</head>
<body>
<script language="javascript">
		function create_object()
			{
			var my_obj = false ;
			try
				{
				my_obj = new XMLHTTPRequest();/*firefox*/
				}
				
			catch(err1)
				{
				try
					{
					my_obj = new ActiveXObject("MIcrosoft.XMLHTTP");
					}
					
				catch(err2)
					{
					try
						{
						my_obj = new ActiveXObject("XML2.XMLHTTP");
						}
						
					catch(err3)
						{
						my_obj = false ;
						}	
					}
				}	
			return my_obj;	
			}	
		//--------------->>>>
		function walaa()
			{
			if(my_obj.readystate == 4)
				{
     document.getElementById("mydiv").innerHTML = my_obj.responseText;
	document.mydiv.innerHTML=my_obj.responseText;;
				window.alert('Message goes here');
				}	
			else
				{
				document.getElementById("mydiv").innerHTML = "" ;
				}
			}
			
			
		var my_obj = create_object();
		
				//document.write(my_obj.responseText);
		
		function start_ajax()
		{	
		//var soma = 'looooooka';	
		var url='respone.php';
		
	
				
        my_obj.open('GET',url,true);
		my_obj.onreadystatechange = walaa;
		my_obj.send(null);	
		}
		
		
	</script>

<form method='' action=''>

<input type="button" value="Insert"  önClick="start_ajax();" />
<input type='text' name='username'  önBlur="start_ajax();" />

</form>

</body>

</html>

推荐答案



上面的代码中有错误:

1. document.getElementById("mydiv")将始终返回null,因为页面上没有具有该id的元素.
2.önClick应该是onClick
3.önBlur应该是onBlur


从样品划出的线不正确.请这样尝试:
Hi,

There are errors in the code above:

1. document.getElementById("mydiv") will always return null because there is no element with that id on the page.
2. önClick should be onClick
3. önBlur should be onBlur


The line struck out from the sample was not correct. Please try it like this:
function walaa()
{
  if(my_obj.readystate == 4)
  {
    document.getElementById("mydiv").innerHTML = my_obj.responseText;
    document.mydiv.innerHTML=my_obj.responseText;
    window.alert('Message goes here');
  }
  else
  {
    document.getElementById("mydiv").innerHTML = "<img src="loader.gif" />" ;
  }
}



* 1 SP ==解决方案提供商
[/编辑]

嗯,我可以在您发布的代码中看到MyDiv放置在表单标签之外,但是如果您将MyDiv放置在代码中的表单标签内,并且response.php返回一些带有表单标签的html,它将失败!如果不是这种情况,请发布由response.php返回的html示例.

问候
约阿希姆(Joachim)



*1 SP == Solution Provider
[/Edit]

Hmmm I can see in the code you have posted that the MyDiv is placed outside of the form tags, but if you have placed it inside the form tag in your code and the response.php returns some html with a form tag, it will fail! If this is not the case please post a sample of the html returned by the response.php.

Regards
Joachim


这篇关于Ajax不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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