在 jquery 中从 $.get ajax 调用获取变量

发布于 2024-11-08 10:48:58 字数 398 浏览 0 评论 0 原文

我知道有很多关于这个的线程..但我对ajax很糟糕,并且无法为我的生活解释答案。

var loadUrl = "lib/php/ajax.php";

$.ajaxSetup ({ cache: false });

$.get(loadUrl +  href, function(data)
{
    var obj = $.parseJSON(data);
    console.log(obj.commentCount, obj.fileName);            
});  console.log(obj.commentCount, obj.fileName); // this one doesn't work.

我如何在函数之外获取这些变量=/我知道我需要使用某种回调函数..或者其他东西

I know there are a LOT of threads on this.. but I suck bad at ajax and cannot for the life of me interpret the answers.

var loadUrl = "lib/php/ajax.php";

$.ajaxSetup ({ cache: false });

$.get(loadUrl +  href, function(data)
{
    var obj = $.parseJSON(data);
    console.log(obj.commentCount, obj.fileName);            
});  console.log(obj.commentCount, obj.fileName); // this one doesn't work.

How do I get those variables outside of the function =/ i know i need to use some kind of callback function .. or something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如若梦似彩虹 2024-11-15 10:48:58

不能这样做有两个原因:

  1. $.get 之后的代码会立即运行。函数内部的代码仅在 ajax 响应返回后运行。
  2. var obj 是在内部函数的范围内创建的,因此外部无法访问。

让它工作的唯一方法是在 ajax 请求之外声明 obj,在函数内部分配值,并将 ajax 请求设置为非异步(请参阅 jquery Ajax) 虽然这有点违背了使用 ajax 的目的,并且会阻止网站直到结果返回。

You can't for two reasons:

  1. The code after the $.get is run instantly. The code inside the function is only run after the ajax response returns
  2. The var obj is created inside the scope of the inner function so is not accessible outside.

The only way you can get this to work is to declare obj outside of the ajax request, assign the value inside the function and also set the ajax request to not be asyncronous (See jquery Ajax) Although this kinda defeats the point of using ajax and will block the site until the result comes back.

自此以后,行同陌路 2024-11-15 10:48:58

ajax 调用完成后执行回调。您的最后一个 console.log() 语句将首先被调用,因为 ajax 请求是异步发生的。另一个问题是 obj 属于回调的范围,这使得它在外部不可用。如果您在回调之外声明它,它将可用,但由于第一个问题仍然无法使用。它将会是空的,直到 ajax 调用完成。在回调中进行工作有什么问题?

The callback gets executed after the ajax call has finished. Your last console.log() statement is going to be called first, because the ajax request happens asynchronously. The other problem is that obj belongs to the scope of the callback, which makes it unavailable on the outside. If you declare it outside of the callback, it'll be available but still unusable because of the first problem. It is going to be empty, until the ajax call has finished. What is the problem with doing the work in the callback?

小梨窩很甜 2024-11-15 10:48:58

先在函数外部声明var obj,然后可以将它设置为内部数据,然后在外部引用它。

var loadUrl = "lib/php/ajax.php";
var obj = "";

$.ajaxSetup ({ cache: false });

$.get(loadUrl +  href, function(data)
{
    obj = $.parseJSON(data);
    console.log(obj.commentCount, obj.fileName);            
});
console.log(obj.commentCount, obj.fileName);

Declare var obj first before, outside the function, then you can set it = to the data inside, and reference it outside after.

var loadUrl = "lib/php/ajax.php";
var obj = "";

$.ajaxSetup ({ cache: false });

$.get(loadUrl +  href, function(data)
{
    obj = $.parseJSON(data);
    console.log(obj.commentCount, obj.fileName);            
});
console.log(obj.commentCount, obj.fileName);
携君以终年 2024-11-15 10:48:58
var loadUrl = "lib/php/ajax.php";

function processAjaxResponse(obj)
{
  console.log(obj.commentCount, obj.fileName);
};

$.ajaxSetup ({ cache: false });

$.ajax({
    type: 'GET',
    dataType: 'JSON',
    url: loadUrl +  href,
    success: function(data) {
       processAjaxResponse(data);
   }
});
var loadUrl = "lib/php/ajax.php";

function processAjaxResponse(obj)
{
  console.log(obj.commentCount, obj.fileName);
};

$.ajaxSetup ({ cache: false });

$.ajax({
    type: 'GET',
    dataType: 'JSON',
    url: loadUrl +  href,
    success: function(data) {
       processAjaxResponse(data);
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文