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

时间:2022-10-08 08:52:49

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.

我知道这有很多线索..但我在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.

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

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

4 个解决方案

#1


0  

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?

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

#2


2  

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. $ .get之后的代码立即运行。函数内部的代码仅在ajax响应返回后运行
  3. The var obj is created inside the scope of the inner function so is not accessible outside.
  4. var obj是在内部函数的范围内创建的,因此无法在外部访问。

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.

你可以让它工作的唯一方法是在ajax请求之外声明obj,在函数内部分配值,并将ajax请求设置为不同步(参见jquery Ajax)虽然这样做有点失败了使用ajax和将阻止该网站,直到结果回来。

#3


-1  

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

首先声明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);

#4


-1  

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);
   }
});

#1


0  

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?

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

#2


2  

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. $ .get之后的代码立即运行。函数内部的代码仅在ajax响应返回后运行
  3. The var obj is created inside the scope of the inner function so is not accessible outside.
  4. var obj是在内部函数的范围内创建的,因此无法在外部访问。

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.

你可以让它工作的唯一方法是在ajax请求之外声明obj,在函数内部分配值,并将ajax请求设置为不同步(参见jquery Ajax)虽然这样做有点失败了使用ajax和将阻止该网站,直到结果回来。

#3


-1  

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

首先声明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);

#4


-1  

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);
   }
});