簡體   English   中英

Ajax不調用回調函數

[英]Ajax not calling callback function

我正在嘗試從服務器上端口8080上運行的服務獲取JSON對象。 我已經實現了以下JavaScript和PHP代碼以實現此目的:

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

但是,即使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} 

因此,事實證明,函數聲明的順序與Ajax調用有關。 誰知道^^

我在Ajax調用之后定義了回調函數。 我把它們轉過來,現在工作正常。

感謝您的答復。

$.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 );
    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM