var temp;
$.ajax({
async: false,//已经是同步请求了
dataType: "json",
data: { act: "G" },
url: Map_Server,//远程地址
error: function (XmlHttpRequest, textStatus, errorThrown) { },
success: function (d) {
if (d.R == "1") {
temp = d.T;
} else {
temp = null;
}
alert(temp + "/");
}
});
alert(temp + "|");
return temp;为什么结果是: undefined|
[object Object]/
[object Object] //如果中间没有alert的话,返回的是undefined
我已经设置同步请求了。
同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。难道这句话,过时了,不中用了?????
$.ajax({
async: false,//已经是同步请求了
dataType: "json",
data: { act: "G" },
url: Map_Server,//远程地址
error: function (XmlHttpRequest, textStatus, errorThrown) { },
success: function (d) {
if (d.R == "1") {
temp = d.T;
} else {
temp = null;
}
alert(temp + "/");
}
});
alert(temp + "|");
return temp;为什么结果是: undefined|
[object Object]/
[object Object] //如果中间没有alert的话,返回的是undefined
我已经设置同步请求了。
同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。难道这句话,过时了,不中用了?????
加一个cache:false,global:false, 试试看
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
var Map_Server = "test.asp";
function doit(){
var temp;
$.ajax({
async:false,//已经是同步请求了
dataType:"json",
cache:false,
global:false,
data: { act: "G" },
url: Map_Server,//远程地址
success:function(d){
if (d.R == "1") {
temp = d.T;
} else {
temp = null;
}
alert(temp + "/");
}
});
alert(temp + "|");
return temp;
}
alert(doit())
</script>
跨域的话,通过后台代理取得后再返回给客户端.
var html = $.ajax({
url: "some.php",
async: false
}).responseText;
如果不是跨域, 请楼主检查你后台返回的数据是否正确
var str="White";
var SendAjax=new Ajax.Request('test.txt',{onComplete:RETURN_RESULT});
function RETURN_RESULT(res){ //此函数需要在ajax完成请求过程之后才会被执行
str=res.responseText;
}//该查询结果为“blue”;
alert(str);//而alert是在fn函数被调用时就执行了,也就是说这里的alert在RETURN_RESULT函数运行之前就执行了,所以str变改还没被改写,可以用setTimeout之类控制一下。但不推荐这样控制,因为ajax响应的时间是未知数
//str.print();
//setTimeout('str.print()',1000);
}
http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/
async"jsonp" requests do not support synchronous operation
$.ajax({
async: false, //已经是同步请求了
method: 'get',
cache: false,
global: false,
dataType: "json",
data: {},
url: 'http://www.baidu.com/test.aspx?jsoncallback=?', //远程地址
error: function (XmlHttpRequest, textStatus, errorThrown) { alert(textStatus + "/" + errorThrown); },
success: function (d) {
//alert('success');
temp = d.name
//alert('inside' + temp);
}
});
alert('out' + temp);/*怎么保证“此时”,temp这个值,是正确的?*/
setTimeout(function () { alert('out' + temp); }, 5000);/*经过几秒的延迟,temp这个值,就是想要的值的。*/
哦。那这样怎么办?怎么才能保证下一句js代码调用 不为为null 或者 undefined
最好的方法还是放到success回调函数里面