1

我正在尝试从在我的服务器上的端口 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} 
4

2 回答 2

1

所以事实证明,函数声明的顺序与 Ajax 调用有关。谁知道^^

我在 Ajax 调用之后定义了回调函数。我把它们换了,现在它工作正常。

感谢您的回复。

于 2013-08-09T02:26:16.773 回答
0
$.ajax({
    url: 'test.php',
    dataType: 'html',
    data: { test: 'test' },
    type: 'GET',
    success: function( data ) {
        console.log( 'success' );
        $( '#div2' ).html( data );
    },
    error: function( xhr ) {
        console.log( xhr.status );
    }

});

或者像这样

<?php
$url = "http://localhost:8080/media_info/" . $_GET['file'];
echo json_encode( Array(
    'data': file_get_contents( $url )
) );
?>

javascript

$.ajax({
    url: 'test.php',
    dataType: 'json',
    data: { file: stream_ },
    type: 'GET',
    success: function( callback) {
        console.log( 'success' );
        $( '#div2' ).html( callback.data );
    },
    error: function( xhr ) {
        console.log( xhr.status );
    }

});
于 2013-08-09T01:57:43.970 回答