简体   繁体   English

如何从jQuery $ .get方法读取返回的对象

[英]how can I read returned object from jQuery $.get method

I was wondering how to extract data from a returned object via jQuery's $.get() method. 我想知道如何通过jQuery的$ .get()方法从返回的对象中提取数据。 IE: IE浏览器:

function dynamicData(file){
    var wantedData;
    var getObj = $.get(file);
    wantedData = getObj.complete(function(data){return data;});
    return wantedData;
}
$(window).load(function(){
     var newData = dynamicData('somefile');
     alert(newData);
});

I don't want to just stick the data to some DOM as soon as it's gotten the new data. 我不想在获取新数据后立即将数据粘贴到某些DOM。

I get an object alerted, but how do I get data inside of it? 我收到对象警报,但是如何在其中获取数据? I have no idea how the object structure is at this point since newData is an object, but newData[0] is null. 我不知道此时的对象结构如何,因为newData是一个对象,但是newData [0]为null。 Is this by chance some sort of mapped object with key:value pairs? 这是不是偶然有键:值对的某种映射对象? or are we not allowed to do it this way? 还是我们不允许这样做?

You can't return from an Ajax call. 您无法从Ajax调用返回。 It's Asynchronous. 它是异步的。

Do whatever you want to do with the data in the success callback. 对成功回调中的数据执行任何您想做的事情。

See the manual for get , which has examples. 有关示例,请参见get手册

$.get requires a callback function that will receive the data as soon as it is done loading. $ .get需要一个回调函数,该函数将在加载完成后立即接收数据。 It will not directly return the data to the calling function! 它不会直接将数据返回给调用函数!

Please refer to the jQuery.get documentation for more information. 请参考jQuery.get文档以获取更多信息。

Since $.get() is asynchronous, you're calling your alert before the get returns any data. 由于$ .get()是异步的,因此您要在get返回任何数据之前调用警报。

A better approach would be: 更好的方法是:

function dynamicData(file,callback){
  $.get(file,function(data){
    callback(data);
  });
}

$(window).load(function(){
  dynamicData('somefile', alert);
});

which will alert(data) when it becomes available. 当数据可用时,它将发出警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从javascript中返回的对象引用私有方法? - How can i reference a private method from returned object in javascript? 我无法使用ajax方法和jQuery从操作中返回模型 - I can't get model returned back from action with ajax method and jQuery 如何基于从php返回的日期对象在jquery中创建新的日期对象? - how can I create a new Date object in jquery based on the date object returned from php? 如何在JQuery中做到这一点? (获取从AJAX-GET返回的HTML,并将其变成一个对象) - How do I do this in JQuery? (get the HTML returned from AJAX-GET and turn it into an object) 如何在jQuery中读取“ .on()”方法参数? - How can I read “.on()” method arguments in jQuery? 如何从jQuery中从Ajax调用返回的数据中获取对象? - How to get object from data returned from Ajax call in jQuery? 如何修改jQuery的html()方法的返回值? - How can I modify the returned value of jQuery's html() method? 如何从包含jQuery.get()的JavaScript函数中获取返回值? - How can I get returned value from JavaScript function that contain jQuery.get()? 从 Jquery 获取返回的数据 每个 object 返回的 Object - Get returned data from Jquery Returned Object for each object 我可以在返回新对象之前从新对象获取数据吗? - Can I get data from a new object before it is returned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM