1

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} 
6
  • What does the 'success' callback code look like? Commented Aug 9, 2013 at 1:45
  • Your PHP code seems to present a vulnerability. Imagine if I try to access to mediainfo.php?file=../private/file_i_should_not_see. Commented Aug 9, 2013 at 1:47
  • Can you post a response of a success call Commented Aug 9, 2013 at 1:53
  • maybe because it's not successful after all Commented Aug 9, 2013 at 2:01
  • @Pikrass Thanks for the response. The PHP code will only respond with basic info about a media file. Do you think this still presents a vulnerability? Commented Aug 9, 2013 at 2:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If your play function was written like so: function play(){...} rather than using var play = function... Your problem wouldn't have existed. One of the weird things about javascript.
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 );
    }

});

or like this

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

});

1 Comment

This doesn't answer the question ("why isn't the success callback being called?"). You've proposed methods for in-code debugging, but haven't explained it to the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.