Php Ajax不调用回调函数

Php Ajax不调用回调函数,php,javascript,jquery,ajax,json,Php,Javascript,Jquery,Ajax,Json,我试图从服务器端口8080上运行的服务中获取JSON对象。我已经实现了以下JavaScript和PHP代码来实现这一点: var play = function( info ) { if ( info.width && info.height ) { while ( info.width < 640 ) { info.width = Math.round( info.width * 1.5 );

我试图从服务器端口8080上运行的服务中获取JSON对象。我已经实现了以下JavaScript和PHP代码来实现这一点:

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"});
  }
JavaScript:

$.ajax({
    type: 'GET',
    url: "mediainfo.php?file="+stream_,
    dataType: 'json',
    success: play,
    error: function( xhr, reply ) {
       play({});
    }
});
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"});
  }
mediainfo.php:

<?php
    $url = "http://localhost:8080/media_info/" . $_GET['file'];
    echo file_get_contents($url);
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"});
  }
还是像这样

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"});
  }
<?php
$url = "http://localhost:8080/media_info/" . $_GET['file'];
echo json_encode( Array(
    'data': file_get_contents( $url )
) );
?>
还是像这样

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

结果证明,函数声明的顺序与Ajax调用有关。谁知道呢^^

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"});
  }
我在Ajax调用之后定义了回调函数。我把它们调换了,现在它可以正常工作了

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"});
  }

感谢您的回复。

因此,函数声明的顺序与Ajax调用有关。谁知道呢^^

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"});
  }
我在Ajax调用之后定义了回调函数。我把它们调换了,现在它可以正常工作了

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"});
  }

感谢您的回复。

成功回调代码是什么样子的?您的PHP代码似乎存在漏洞。想象一下,如果我尝试访问
mediainfo.php?file=../private/file\u我不应该看到
。你能发布一个成功呼叫的响应吗?可能是因为它之后不成功all@Pikrass谢谢你的回复。PHP代码将只响应媒体文件的基本信息。您认为这仍然存在漏洞吗?“成功”回调代码是什么样子的?您的PHP代码似乎存在漏洞。想象一下,如果我尝试访问
mediainfo.php?file=../private/file\u我不应该看到
。你能发布一个成功呼叫的响应吗?可能是因为它之后不成功all@Pikrass谢谢你的回复。PHP代码将只响应媒体文件的基本信息。您认为这仍然存在漏洞吗?这并不能回答问题(“为什么没有调用成功回调?”)。您已经提出了代码内调试的方法,但没有向OP解释。这并没有回答问题(“为什么没有调用成功回调?”)。您已经提出了代码内调试的方法,但没有向OP解释。如果您的play函数是这样编写的:
function play(){…}
而不是使用
var play=function…
您的问题就不会存在。javascript的一个奇怪之处是:如果你的play函数是这样写的:
function play(){…}
而不是使用
var play=function…
你的问题就不会存在了。javascript的一个奇怪之处。
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"});
  }