jQuery:执行同步 AJAX 请求
我过去曾做过一些 jQuery,但我完全陷入困境。我知道使用同步 ajax 调用的优点和缺点,但在这里它是必需的。
远程页面已加载(由 firebug 控制),但未显示返回。
我应该采取什么措施才能使我的函数正确返回?
function getRemote() {
var remote;
$.ajax({
type: "GET",
url: remote_url,
async: false,
success : function(data) {
remote = data;
}
});
return remote;
}
I've done some jQuery in the past, but I am completely stuck on this. I know about the pros and cons of using synchronous ajax calls, but here it will be required.
The remote page is loaded (controlled with firebug), but no return is shown.
What should I do different to make my function to return properly?
function getRemote() {
var remote;
$.ajax({
type: "GET",
url: remote_url,
async: false,
success : function(data) {
remote = data;
}
});
return remote;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您发出同步请求时,应该是
示例 - http://api。 jquery.com/jQuery.ajax/#example-3
请注意:将 async 属性设置为 false 已弃用,并且正在被删除(链接)。如果您使用以下命令,包括 Firefox 和 Chrome 在内的许多浏览器已经开始在控制台中打印警告:
Chrome:
火狐浏览器:
As you're making a synchronous request, that should be
Example - http://api.jquery.com/jQuery.ajax/#example-3
PLEASE NOTE: Setting async property to false is deprecated and in the process of being removed (link). Many browsers including Firefox and Chrome have already started to print a warning in the console if you use this:
Chrome:
Firefox:
您错误地使用了 ajax 函数。由于它是同步的,它将返回内联数据,如下所示:
You're using the ajax function incorrectly. Since it's synchronous it'll return the data inline like so:
该网址有多远?是来自同一个域吗?代码看起来不错
试试这个
how remote is that url ? is it from the same domain ? the code looks okay
try this