Ind*_*ial 178 ajax jquery synchronous
我过去做过一些jQuery,但我完全坚持这个.我知道使用同步ajax调用的优点和缺点,但在这里它将是必需的.
加载远程页面(使用firebug控制),但不显示任何返回.
我应该做些什么让我的功能正常返回?
function getRemote() {
var remote;
$.ajax({
type: "GET",
url: remote_url,
async: false,
success : function(data) {
remote = data;
}
});
return remote;
}
Run Code Online (Sandbox Code Playgroud)
Dog*_*ert 290
当你正在进行同步请求时,应该是这样
function getRemote() {
return $.ajax({
type: "GET",
url: remote_url,
async: false
}).responseText;
}
Run Code Online (Sandbox Code Playgroud)
示例 - http://api.jquery.com/jQuery.ajax/#example-3
请注意:异步属性设置为false被弃用,并要被删除(过程链接).如果您使用此功能,许多浏览器(包括Firefox和Chrome)已经开始在控制台中打印警告:
铬:
主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用.如需更多帮助,请查看https://xhr.spec.whatwg.org/.
火狐:
主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用.如需更多帮助http://xhr.spec.whatwg.org/
Jak*_*ake 32
你错误地使用了ajax函数.由于它是同步的,它将像这样返回内联数据:
var remote = $.ajax({
type: "GET",
url: remote_url,
async: false
}).responseText;
Run Code Online (Sandbox Code Playgroud)
The*_*ain 17
这个网址有多远?它是来自同一个域吗?代码看起来还不错
试试这个
$.ajaxSetup({async:false});
$.get(remote_url, function(data) { remote = data; });
// or
remote = $.get(remote_url).responseText;
Run Code Online (Sandbox Code Playgroud)