dp6319 2013-08-09 01:42
浏览 129
已采纳

Ajax没有调用回调函数

I am trying to get a JSON object from a service running on port 8080 on my server. I have implemented the following JavaScript and PHP code to achieve this:

JavaScript:

$.ajax({
    type: 'GET',
    url: "mediainfo.php?file="+stream_,
    dataType: 'json',
    success: play,
    error: function( xhr, reply ) {
       play({});
    }
});

mediainfo.php:

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

However even if the Ajax call succeeds it never calls the callback. Strangely if it fails (e.g. if $url does not return valid JSON) it does call the callback.

I can't figure out what's going wrong. Any help would be much appreciated.

edit:

callback function:

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

response from mediinfo.php:

{"duration":69960.0,"width":720,"height":406} 
  • 写回答

2条回答 默认 最新

  • dongyu8664 2013-08-09 02:26
    关注

    So it turns out that the order of function declarations matter with Ajax calls. Who knew ^^

    I had my callback function defined after the Ajax call. I switched them round and now it works fine.

    Thanks for the responses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R