阿贾克斯的responseText回来为未定义 [英] Ajax responseText comes back as undefined
问题描述
我有问题,这片code;返回值回来为未定义。这是什么问题?
VAR FX = NULL;
xmlhttp.open(GET,网址,虚假);
xmlhttp.onreadystatechange =功能()
{
警报(进入FUNC);
如果(xmlhttp.readyState == 4)
{
如果(xmlhttp.status == 200)
{
警报(FX);
FX = xmlhttp.responseText;
返回外汇;
}
其他
{
警报(错误+ xmlhttp.statusText);
}
}
}
较新的code:
函数的getData(回调)
{
xmlhttp.open(GET,网址,虚假);
xmlhttp.onreadystatechange =功能()
{
如果(xmlhttp.readyState == 4)
{
如果(xmlhttp.status == 200)
{
警报(xmlhttp.responseText);
cbfunc(xmlhttp.responseText);
}
其他
{
警报(错误+ xmlhttp.statusText);
}
}
}
xmlhttp.send(空);
}
如何我打电话吧:
的getData(功能cbfoo(TXT)
{
//document.form.autodate.value=txt;
警报(TXT);
警报(document.form.autodate.value);
});`
根据您的编辑更新
函数的getData(回调)
{
//你应该将创建XMLHTTP在这里
如果需要的话//这样就可以使多个的getData电话
//如果你保持XMLHTTP函数外部不同的呼叫的getData会干扰
// 与彼此
xmlhttp.open(GET,网址,虚假); //错误应该是真实的,使之异步
...
{
警报(xmlhttp.responseText);
cbfunc(xmlhttp.responseText); //你的函数会得到作为
//参数回调,但你
//使用,而不是cbfunc这里的回调
...
的getData(功能cbfoo(TXT)//你可以在这里省略函数名
...
修复这些问题,应该使code的工作。
旧的答案
您正在呼叫的XMLHtt prequest在同步模式,这意味着它将阻止脚本直到请求已经完成,因为你分配阻塞调用后的的onreadystatechange
回调(这意味着以后的请求已完成),您的code从来没有得到通知。
由于同步模式块剧本,而且还阻碍了浏览器的用户界面,所以不推荐使用此模式。
您应该(为99%的情况下),使用异步模式,并使用一个回调来处理数据,因为 xmlhttp.open
不可以返回的onreadystatechange
回调的返回值,它只是返回未定义
时立即做在异步模式下运行。
现在一个常见的模式就是写一个包装的要求,并通过了匿名功能,这个包装,以后将调用回来的时候请求已完成。
函数doRequest(URL,回调){
VAR XMLHTTP = ....; //这里创建一个新的请求
xmlhttp.open(GET,URL,真正的); //为异步
xmlhttp.onreadystatechange =功能(){
如果(xmlhttp.readyState == 4){
如果(xmlhttp.status == 200){
//通过响应回调函数
回调(NULL,xmlhttp.responseText);
} 其他 {
//传递错误的回调函数
回调(xmlhttp.statusText);
}
}
}
xmlhttp.send(空);
}
您现在可以做的请求,并提供一个函数,会得到尽快完成请求调用该函数,那么你做任何你想做的事与响应的内部。
doRequest(http://mysite.com/foo',功能(ERR,响应){//传递一个匿名函数
如果(ERR){
警报(错误:+ ERR);
} 其他 {
警报(回应:+响应);
}
});
这是在浏览器中常见的编程模型,始终以异步的解决方案去,如果您阻止脚本你挡住了整个浏览器。
I'm having problems with this piece of code; the return value comes back as 'undefined'. What's the problem?
var fx = null;
xmlhttp.open("GET", URL ,false);
xmlhttp.onreadystatechange=function()
{
alert("enter func");
if (xmlhttp.readyState==4)
{
if (xmlhttp.status == 200)
{
alert(fx);
fx = xmlhttp.responseText;
return fx;
}
else
{
alert("Error" + xmlhttp.statusText);
}
}
}
Newer code:
function getData(callback)
{
xmlhttp.open("GET", URL ,false);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
cbfunc(xmlhttp.responseText);
}
else
{
alert("Error" + xmlhttp.statusText);
}
}
}
xmlhttp.send(null);
}
How I'm calling it:
getData( function cbfoo(txt)
{
//document.form.autodate.value=txt;
alert(txt);
alert(document.form.autodate.value);
});`
Update based on your edit
function getData(callback)
{
// you should move the creation of xmlhttp in here
// so you can make multiple getData calls if needed
// if you keep xmlhttp outside the function the different calls to getData will interfere
// with each other
xmlhttp.open("GET", URL ,false); // false should be true, to make it async
...
{
alert(xmlhttp.responseText);
cbfunc(xmlhttp.responseText); // your function gets passed as the
// parameter "callback" but you're
// using "cbfunc" here instead of "callback"
...
getData(function cbfoo(txt) // you can omit the function name here
...
Fixing those issues should make the code work.
Old answer
You're calling the XMLHttpRequest in Synchronous mode, that means that it will block the script until the request has finished, since you're assigning the onreadystatechange
callback after the blocking call (that means after the request has already finished) your code never gets notified.
Since the synchronous mode blocks the script, it also blocks the Browser's UI, so it's not recommended to use this mode.
You should (for 99% of the cases) use the Asynchronous mode and use a callback to handle the data, since xmlhttp.open
does not return the return value of the onreadystatechange
callback, it simply returns undefined
immediately when run in async mode.
Now a common pattern is to write a wrapper for the request and pass an anonymous function to this wrapper, which later will get called back when the request has finished.
function doRequest(url, callback) {
var xmlhttp = ....; // create a new request here
xmlhttp.open("GET", url, true); // for async
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
// pass the response to the callback function
callback(null, xmlhttp.responseText);
} else {
// pass the error to the callback function
callback(xmlhttp.statusText);
}
}
}
xmlhttp.send(null);
}
You can now do a request and supply a function that will get called as soon as the request finishes, inside of that function you then do whatever you want to do with the response.
doRequest('http://mysite.com/foo', function(err, response) { // pass an anonymous function
if (err) {
alert('Error: ' + err);
} else {
alert('Response: ' + response);
}
});
This is the common programming model in the browser, always go with a asynchronous solution, if you block the script you block the whole Browser.
这篇关于阿贾克斯的responseText回来为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!