我正在尝试从在我的服务器上的端口 8080 上运行的服务获取 JSON 对象。我已经实现了以下 JavaScript 和 PHP 代码来实现这一点:
JavaScript:
$.ajax({
type: 'GET',
url: "mediainfo.php?file="+stream_,
dataType: 'json',
success: play,
error: function( xhr, reply ) {
play({});
}
});
媒体信息.php:
<?php
$url = "http://localhost:8080/media_info/" . $_GET['file'];
echo file_get_contents($url);
但是,即使 Ajax 调用成功,它也不会调用回调。奇怪的是,如果它失败(例如,如果 $url 没有返回有效的 JSON),它确实会调用回调。
我不知道出了什么问题。任何帮助将非常感激。
编辑:
回调函数:
var play = function( info ) {
if ( info.width && info.height ) {
while ( info.width < 640 ) {
info.width = Math.round( info.width * 1.5 );
info.height = Math.round( info.height * 1.5 );
}
while( info.width > 1024 ) {
info.width = Math.round( info.width / 2 );
info.height = Math.round( info.height / 2 );
}
}
var width = info && info.width || 640;
var height = info && info.height || 480;
var flashvars = {
file : stream,
streamer : "rtmp://myserver.com:1935/vodplayback",
'rtmp.tunneling' : false,
bufferlength : 5,
autostart : true
};
var paramObj = {allowfullscreen : "true", allowscriptaccess : "always"};
swfobject.embedSWF("http://myserver.com:8080/flu/jwplayer.swf", "videoplayer", width, height, "10.3", false, flashvars, paramObj, {id : "jwplayer", name : "jwplayer"});
}
来自 mediinfo.php 的回复:
{"duration":69960.0,"width":720,"height":406}