Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中访问ajax调用之外的JSONP数据_Javascript_Json_Jquery_Jsonp - Fatal编程技术网

Javascript 在jQuery中访问ajax调用之外的JSONP数据

Javascript 在jQuery中访问ajax调用之外的JSONP数据,javascript,json,jquery,jsonp,Javascript,Json,Jquery,Jsonp,现在,搜索结果前5页中的每个谷歌链接都是:在我的浏览器中访问过,我需要问 如何使JSON数据正常工作,以便可以用其他方法访问/操作它 \u其他方法:函数(){ //我希望在何处提供此服务的最终目标 var text=this._requestText(); }, _requestText:function(){ var url='1〕http://api.flickr.com/services/feeds/photos_public.gne?format=json'; $.ajax({ 键入:“

现在,搜索结果前5页中的每个谷歌链接都是:在我的浏览器中访问过,我需要问

如何使JSON数据正常工作,以便可以用其他方法访问/操作它

\u其他方法:函数(){
//我希望在何处提供此服务的最终目标
var text=this._requestText();
},
_requestText:function(){
var url='1〕http://api.flickr.com/services/feeds/photos_public.gne?format=json';
$.ajax({
键入:“GET”,
url:url,
async:false,
数据类型:“jsonp”,
成功:功能(数据){
//在这里工作
控制台日志(数据);
//也在这里工作&激发本地功能
测试(数据);
//不储存
var testvar_1=数据;
}
});
//未定义
console.log(testvar_1);
功能测试(数据){
//从上面调用时记录日志
控制台日志(数据);
//不储存
var testvar_2=数据;
}
//未定义
console.log(testvar_2);
//我还没找到这个。。。
返回magicvariablethattletsmeaccessjson
}, ...
有什么想法吗?我知道在堆栈溢出方面还有很多类似的问题,但我没有找到任何解决这个问题的方法

谢谢

更新

var storage;
var yourobj = {
    _otherMethod: function() {
      // END GOAL OF WHERE I WANT THIS TO BE AVAILABLE
      var text = this._requestText();
    },
    _requestText: function() {
      var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
      $.ajax({
          type: 'GET',
          url: url,
          async: false,
          dataType: 'jsonp',
          success: function(data) {
            storage = data;

            // logs correctly
            console.log(storage);
          }
      });
    }
}
//undefined
console.log(storage);
yourobj._requestText();
//undefined
console.log(storage);

通过在变量名之前添加
var
,可以在当前范围内创建一个局部变量

这不起作用:

var a = 2;

(function() {
    var a = 3;
})();

console.log(a); // 2
虽然这样做:

var a = 2;

(function() {
    a = 3;
})();

console.log(a); // 3

由于您试图设置的变量位于外部作用域中,因此在内部作用域中使用它时,请去掉
var

通过在变量名称之前添加
var
,可以在当前作用域中创建局部变量

这不起作用:

var a = 2;

(function() {
    var a = 3;
})();

console.log(a); // 2
虽然这样做:

var a = 2;

(function() {
    a = 3;
})();

console.log(a); // 3

由于您试图设置的变量位于外部作用域中,因此在内部作用域中使用它时,请去掉
var

非常简单。您需要回调函数上下文之外的存储变量

var storage;

var yourobj = {

    _otherMethod: function() {

      // END GOAL OF WHERE I WANT THIS TO BE AVAILABLE
      var text = this._requestText();
    },

    _requestText: function() {
      var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
      $.ajax({
          type: 'GET',
          url: url,
          async: false,
          dataType: 'jsonp',
          success: function(data) {

            storage = data;

          }
      });


    }

}

或者,存储可以是同一对象上的属性。

非常简单。您需要回调函数上下文之外的存储变量

var storage;

var yourobj = {

    _otherMethod: function() {

      // END GOAL OF WHERE I WANT THIS TO BE AVAILABLE
      var text = this._requestText();
    },

    _requestText: function() {
      var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
      $.ajax({
          type: 'GET',
          url: url,
          async: false,
          dataType: 'jsonp',
          success: function(data) {

            storage = data;

          }
      });


    }

}

或者,存储可以是同一对象上的属性。

首先,如其他地方所述,您需要一个在范围内的变量,其次,您需要确保在调用回调之前未对其求值

确保这一点的唯一方法是在成功回调方法中调用
\u otherMethod

_otherMethod: function(text) {
   //...do what ever you need to do with text
},

_requestText: function() {
  var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
  $.ajax({
      type: 'GET',
      url: url,
      async: false,
      dataType: 'jsonp',
      success: function(data) {
         _otherMethod(data);
      },
      }
  });
}
回调是异步的,这意味着它们在不由代码行序列决定的某个时间点被调用。 如果您知道在执行成功回调之前,使用返回数据的代码永远不会被调用,那么您需要保留数据,您可以将代码更改为

_otherMethod: null, //not strictly needed    
_requestText: function() {
  self = this;
  var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
  $.ajax({
      type: 'GET',
      url: url,
      async: false,
      dataType: 'jsonp',
      success: function(data) {
         self._otherMethod = function(data){
              return function(){
                 //do what you need to with data. Data will be stored
                 //every execution of _otherMethod will use the same data
                 console.log(data);
              }
         }
      },
      }
  });
}

首先,正如其他地方所指出的,您需要一个在范围内的变量,其次,您需要确保在调用回调之前没有对其求值

确保这一点的唯一方法是在成功回调方法中调用
\u otherMethod

_otherMethod: function(text) {
   //...do what ever you need to do with text
},

_requestText: function() {
  var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
  $.ajax({
      type: 'GET',
      url: url,
      async: false,
      dataType: 'jsonp',
      success: function(data) {
         _otherMethod(data);
      },
      }
  });
}
回调是异步的,这意味着它们在不由代码行序列决定的某个时间点被调用。 如果您知道在执行成功回调之前,使用返回数据的代码永远不会被调用,那么您需要保留数据,您可以将代码更改为

_otherMethod: null, //not strictly needed    
_requestText: function() {
  self = this;
  var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
  $.ajax({
      type: 'GET',
      url: url,
      async: false,
      dataType: 'jsonp',
      success: function(data) {
         self._otherMethod = function(data){
              return function(){
                 //do what you need to with data. Data will be stored
                 //every execution of _otherMethod will use the same data
                 console.log(data);
              }
         }
      },
      }
  });
}
可能是这样的:

 _requestText: function() {
   var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
   var testvar_1;
   $.ajax({
     type: 'GET',
     url: url,
     async: false,
     dataType: 'jsonp',
     success: function(data) {
       console.log(data);
       testing(data);

       testvar_1 = data;
     }
   });

   // should work here
   console.log(testvar_1);
实际上,您正在那里创建该
var
的新实例。

可能是这样的:

 _requestText: function() {
   var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json';
   var testvar_1;
   $.ajax({
     type: 'GET',
     url: url,
     async: false,
     dataType: 'jsonp',
     success: function(data) {
       console.log(data);
       testing(data);

       testvar_1 = data;
     }
   });

   // should work here
   console.log(testvar_1);

实际上,你在那里创建了一个新的
var
实例。

好的一点我的坏的一点,我知道当不是很晚的时候,但这并不能真正解决问题。我想我的问题更多的是如何让它与回调函数等一起工作。因为成功函数中的“返回数据”也不起作用好的一点坏的一点,我知道如果不是超晚,但这并不能真正解决问题。我想我的问题更多的是如何让它使用回调函数等,因为success函数中的“returndata”也不起作用