Jquery 将ajax数据存储在变量中

Jquery 将ajax数据存储在变量中,jquery,ajax,Jquery,Ajax,一个简单的ajax调用,php响应一些我需要出现在myAJAXVariable所在的JPayer中的xml文本 我需要类似于php echo的东西,它只打印数据 我只想让myAJAXVariable显示来自getxml2.php的数据 $(document).ready(function() { var myAJAXVariable = $.get("http://www.freeenergymedia.com/getxml2.php", function(data){ retu

一个简单的ajax调用,php响应一些我需要出现在myAJAXVariable所在的JPayer中的xml文本

我需要类似于php echo的东西,它只打印数据

我只想让myAJAXVariable显示来自getxml2.php的数据

$(document).ready(function() {



     var myAJAXVariable = $.get("http://www.freeenergymedia.com/getxml2.php", function(data){
return data; <!--here I am attempting to print the contents of getxml2.php as data-->
});



    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, myAJAXVariable, {
        swfPath: "js",
        supplied: "mp3, oga",
        wmode: "window"
    });

});
$(文档).ready(函数(){
var myAJAXVariable=$.get(“http://www.freeenergymedia.com/getxml2.php,函数(数据){
返回数据;
});
新的jPlayerPlaylist({
jPlayer:#jquery_jPlayer_1“,
CSS选择器存储:“jp容器1”
},Myajax变量{
swfPath:“js”,
提供:“mp3,oga”,
wmode:“窗口”
});
});

由于AJAX的异步特性(这是第一个A所代表的),您应该只在成功回调中使用AJAX调用的结果,否则数据还不会初始化:

$(document).ready(function() {
    $.get("http://www.freeenergymedia.com/getxml2.php", function(data) {
        // Only here you should use the data variable as it is here and only here
        // that it is assigned

        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, data, {
            swfPath: "js",
            supplied: "mp3, oga",
            wmode: "window"
        });
    });
});
或创建一个函数:

var initializejPlayer = function(data) {
    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, data, {
        swfPath: "js",
        supplied: "mp3, oga",
        wmode: "window"
    });
} 
然后触发AJAX调用:

$(document).ready(function() {
    $.get("http://www.freeenergymedia.com/getxml2.php", initializejPlayer);
});

由于AJAX的异步特性(这是第一个A所代表的),您应该只在成功回调中使用AJAX调用的结果,否则数据还不会初始化:

$(document).ready(function() {
    $.get("http://www.freeenergymedia.com/getxml2.php", function(data) {
        // Only here you should use the data variable as it is here and only here
        // that it is assigned

        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, data, {
            swfPath: "js",
            supplied: "mp3, oga",
            wmode: "window"
        });
    });
});
或创建一个函数:

var initializejPlayer = function(data) {
    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, data, {
        swfPath: "js",
        supplied: "mp3, oga",
        wmode: "window"
    });
} 
然后触发AJAX调用:

$(document).ready(function() {
    $.get("http://www.freeenergymedia.com/getxml2.php", initializejPlayer);
});

您还可以考虑使用Ajax方法(它比GET更可配置,读取此线程窗体的其他细节)来编写自己的“GETDATA”函数。然后可以使用函数检索代码中的数据

我个人使用此代码在我的backbone.js应用程序中从url检索json数据:

function getJSONData(myurl) 
var data;
$.ajax({
    async: false, //thats the trick
    url: myurl,
    dataType: 'json',
    success: function(response){
       data = response;
        }
    });
return data;
}
如果需要,可以使用不同的数据类型(xml、json、脚本或html)(请看这里) 然后,在您的示例中,您可以在PlayerPlaylist对象中使用它:

new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_1",
    cssSelectorAncestor: "#jp_container_1"
}, getJSONData("http://www.freeenergymedia.com/getxml2.php"), {
    swfPath: "js",
    supplied: "mp3, oga",
    wmode: "window"
});

希望这有帮助。

你也可以考虑使用Ajax方法(它比GET更可配置,读取这个线程窗体的其他细节)来编写自己的“GETDATA”函数。然后可以使用函数检索代码中的数据

我个人使用此代码在我的backbone.js应用程序中从url检索json数据:

function getJSONData(myurl) 
var data;
$.ajax({
    async: false, //thats the trick
    url: myurl,
    dataType: 'json',
    success: function(response){
       data = response;
        }
    });
return data;
}
如果需要,可以使用不同的数据类型(xml、json、脚本或html)(请看这里) 然后,在您的示例中,您可以在PlayerPlaylist对象中使用它:

new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_1",
    cssSelectorAncestor: "#jp_container_1"
}, getJSONData("http://www.freeenergymedia.com/getxml2.php"), {
    swfPath: "js",
    supplied: "mp3, oga",
    wmode: "window"
});

希望这有帮助。

@alex_borsody,在成功回调中,传递给匿名函数的
数据
变量将包含执行
getxml2.php
的结果。也就是说,您应该知道同源策略限制,它阻止您向不同于原始域的域发送AJAX请求。因此,如果
www.freenergymedia.com
不是您托管应用程序的域,您就不能向它发送AJAX请求。@alex_borsody,在成功回调中,传递给匿名函数的
数据
变量将包含执行
getxml2.php
的结果。也就是说,您应该知道同源策略限制,它阻止您向不同于原始域的域发送AJAX请求。因此,如果
www.freenergymedia.com
不是您托管应用程序的域,则无法向其发送AJAX请求。