[英]Ajax not calling callback function
I am trying to get a JSON object from a service running on port 8080 on my server. 我正在尝试从服务器上端口8080上运行的服务获取JSON对象。 I have implemented the following JavaScript and PHP code to achieve this: 我已经实现了以下JavaScript和PHP代码以实现此目的:
JavaScript: JavaScript的:
$.ajax({
type: 'GET',
url: "mediainfo.php?file="+stream_,
dataType: 'json',
success: play,
error: function( xhr, reply ) {
play({});
}
});
mediainfo.php: 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. 但是,即使Ajax调用成功,它也不会调用回调。 Strangely if it fails (eg if $url does not return valid JSON) it does call the callback. 奇怪的是,如果失败(例如,如果$ url不返回有效的JSON),它将调用回调。
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: 来自mediinfo.php的回复:
{"duration":69960.0,"width":720,"height":406}
So it turns out that the order of function declarations matter with Ajax calls. 因此,事实证明,函数声明的顺序与Ajax调用有关。 Who knew ^^ 谁知道^^
I had my callback function defined after the Ajax call. 我在Ajax调用之后定义了回调函数。 I switched them round and now it works fine. 我把它们转过来,现在工作正常。
Thanks for the responses. 感谢您的答复。
$.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 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.