0

我有一个 AJAX 语句,旨在从 PHP 脚本返回回显输出,输出是 XML。

如果直接导航到 PHP 脚本,它会以我需要的确切格式输出 JSON。

AJAX 请求中的“数据”变量没有正确返回它,即使萤火虫网络选项卡显示状态 200 可以请求请求。

PHP 返回 XML 元素“MP3 和标题”

<?php
    $url = 'http://www.startalkradio.net/?page_id=354';
    $rss = simplexml_load_file($url);
    $items = $rss->channel->item;

    $i = 0;
    $data = array();
    foreach ($items as $item) {
        $data[] = array(
            'title' => (string) $item->title,
            'mp3'   => (string) $item->enclosure['url'],
        );
        if (++$i == 3) break;
    }

    $jsdata = json_encode($data);
    echo htmlspecialchars($jsdata, ENT_NOQUOTES, 'utf-8');
?>

AJAX 调用填充 JPlayer 脚本。data好像没有退货。

$(document).ready(function() {
    $.get(
        "http://www.freeenergymedia.com/getxml2.php", 
        function(data) {
            new jPlayerPlaylist({
                jPlayer: "#jquery_jplayer_1",
                cssSelectorAncestor: "#jp_container_1"
            }, 
            data, 
            {        <!-- here I am returning the php script to populate XML into JPlayer. -->
                swfPath: "js",
                supplied: "mp3, oga",
                wmode: "window"
            });
        }
    );
});

有问题的链接

这是一个有效的版本,请注意 XML 与 PHP 脚本 链接输出的内容相同

4

1 回答 1

1

您说您正在返回 XML,但您的 PHP 使用json_encode(). 因此,您的$.get()电话应指定:

//using `$.getJSON()` will set the dataType property to json so your server-side output will be parsed into a JavaScript object
$.getJSON(
        "http://www.freeenergymedia.com/getxml2.php", 
        function(data) {
            console.log(data);//<--use this to inspect the JSON object returned from the server, make sure it's in the proper format
            new jPlayerPlaylist({
                jPlayer: "#jquery_jplayer_1",
                cssSelectorAncestor: "#jp_container_1"
            }, 
            data, 
            {        <!-- here I am returning the php script to populate XML into JPlayer. -->
                swfPath: "js",
                supplied: "mp3, oga",
                wmode: "window"
            });
        }
    );

data应该是这样的:

data = [
    {"title":"some title", "mp3":"path to some song"},
    {"title":"some other title", "mp3":"path to some other song"},
    etc...
];
于 2011-12-08T17:50:37.127 回答