AJAX响应问题 [英] Problem with AJAX Response

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

问题描述

嗨...
我需要处理更多的AJAX内容,所以我想使用带有URL(对Ping的url),要发送的参数,ctype(内容类型XML,JSON或TEXT)之类的参数的JS函数.我需要的只是当我使用这些参数调用该函数时,该函数应返回XML或JSON对象...但是,在
之外,Obj是无法访问的
http.onreadystatechange = function()
{
if(http.readyState == 4&&http.status == 200)
{
obj = http.responseXML;
}
}
我需要在http.onreadystatechange = function()之外的位置访问"obj",以便可以将其返回给被调用的函数...
现在我在哪里失踪...还是有其他替代方法可以做到这一点...


Hi ...
I Need to work with more AJAX Content, So i want a JS Function with Arguments like URL(the url to Ping), Parameters to Send , ctype(Content Type XML,JSON or TEXT). All i need is when i Call the Function with these Arguments , the Function should return the XML or JSON object back... But the Obj is inaccesible outside the

http.onreadystatechange =function()
{
if(http.readyState == 4 && http.status == 200)
{
obj=http.responseXML;
}
}
i need to access ''obj'' here outside the http.onreadystatechange =function(), SO that i can return it to the Called Function ...
Now where am i Missing ... or is there any Alternative ways to do this ...


var url="getxml.php";
var paramstring="abc=20&def=2";
var ctype="XML";
// URL - URL of PHP, paramstring - Parameters to Send , Ctype-ContentType (Strictly XML,JSON,or TEXT) 
var xmlobj = GetTHTTPObject(url,paramstring,ctype);
alert(xmlobj); // I need to get the obj returned by the AJAX here ...







function GetTHTTPObject(url,paramstring,ctype)
{
		var http = GetXmlHttpRequestObject();					//new XMLHttpRequest();
		var params = paramstring;
//		var obj;
		http.open("POST", url, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
//		if(http.readyState == 4 && http.status == 200)
//		alert(http.responseXML);
		data=http.onreadystatechange =function() //Call a function when the state changes.
		{
			if(http.readyState == 4 && http.status == 200)
			{
			obj=http.responseXML;
			}
			alert(obj);
			return obj;
		}
		http.send(params);
		alert(obj);  // How to get the ''obj'' accessible here ?
                return obj;
}



我该如何解决...

Manikanda raj S



How can i Solve this ...

Manikanda raj S

推荐答案

Manikanda raj S写道:
Manikanda raj S wrote:

http.send(params ); alert(obj); //如何在此处访问"obj"?返回"obj";

http.send(params); alert(obj); // How to get the ''obj'' accessible here ? return "obj";


最后一行.您正在重播名为"obj"的字符串.删除它.我相信这不是必需的.



Last line in this. You are returing a string named ''obj''. Remove this. It''s not required i believe.


Manikanda raj S写道:
Manikanda raj S wrote:

alert(obj); //如何在此处访问"obj"?

alert(obj); // How to get the ''obj'' accessible here ?


为什么要在那里访问?直到除非在readystatechanged方法中未取回已发送的请求,否则您在其中将没有期望的值.



Why you want to access that there? Until unless the sent request is not retrieved back in the readystatechanged method, you wont be having expected value in it.


Manikanda raj S写道:
Manikanda raj S wrote:

我需要在http.onreadystatechange = function()之外访问"obj",以便我可以返回它到被调用的函数...

i need to access ''obj'' here outside the http.onreadystatechange =function(), SO that i can return it to the Called Function ...


全局定义一个变量.说``objResult''.
现在,在您的readystatechanged方法中,将值分配给此全局变量.无需设置返回值.

那应该就是全部.


Define a variable globally. Say ''objResult''.
Now, in your readystatechanged method, assign the value to this global variable. No need to set return value.

That should be all.


这篇关于AJAX响应问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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