Bullgod Bullgod - 3 months ago 18
Ajax Question

NetworkError: 500 Internal Server Error when i call php file with ajax in laravel

i have a problem when i call a php file in to an ajax jquery function in my laravel project ,i receive an error:
POST http://localhost:8000/getvideo.php 500 Internal Server Error
"NetworkError: 500 Internal Server Error - http://localhost:8000/getvideo.php"

this is the view:

@extends('layouts.app')

@section('content')


<div id="jp_container_1" class="jp-video jp-video-270p" role="application" aria-label="media player">
<div class="jp-type-playlist">
<div id="jquery_jplayer_1" class="jp-jplayer"></div>
<div class="jp-gui">
<div class="jp-video-play">
<button class="jp-video-play-icon" role="button" tabindex="0">play</button>
</div>
<div class="jp-interface">
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
<div class="jp-current-time" role="timer" aria-label="time">&nbsp;</div>
<div class="jp-duration" role="timer" aria-label="duration">&nbsp;</div>
<div class="jp-controls-holder">
<div class="jp-controls">
<button class="jp-previous" role="button" tabindex="0">previous</button>
<button class="jp-play" role="button" tabindex="0">play</button>
<button class="jp-next" role="button" tabindex="0">next</button>
<button class="jp-stop" role="button" tabindex="0">stop</button>
</div>
<div class="jp-volume-controls">
<button class="jp-mute" role="button" tabindex="0">mute</button>
<button class="jp-volume-max" role="button" tabindex="0">max volume</button>
<div class="jp-volume-bar">
<div class="jp-volume-bar-value"></div>
</div>
</div>
<div class="jp-toggles">
<button class="jp-shuffle" role="button" tabindex="0">shuffle</button>
<button class="jp-full-screen" role="button" tabindex="0">full screen</button>
</div>
</div>
<div class="jp-details">
<div class="jp-title" aria-label="title">&nbsp;</div>
</div>
</div>
</div>
<div class="jp-playlist">
<ul>
<!-- The method Playlist.displayPlaylist() uses this unordered list -->
<li>&nbsp;</li>
</ul>
</div>
<div class="jp-no-solution">
<span>Update Required</span>
To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
</div>
</div>
</div>
@endsection
@section('page-style-files')

<link href="/assets/vendor/jPlayer-2.9.2/dist/skin/blue.monday/css/jplayer.blue.monday.min.css" rel="stylesheet" type="text/css" />
{{-- <link href="/css/jquery.bxslider.css" rel="stylesheet" /> --}}
@stop

@section('page-js-files')
<script src="{!!url('../js/jquery.min.js')!!}"></script>
<script type="text/javascript" src="../assets/vendor/jPlayer-2.9.2/dist/jplayer/jquery.jplayer.min.js"></script>
<script type="text/javascript" src="../assets/vendor/jPlayer-2.9.2/dist/add-on/jplayer.playlist.min.js"></script>
@stop

@section('page-js-script')
<script type="text/javascript">
//<![CDATA[
var current_clicked_item = $(".jp-play1").eq(0);
$(document).ready(function(){
playlist = new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
});
readMP4();

$(".jp-play1").click(function(event){
current_clicked_item = $(this);
event.preventDefault();
readMP4($(this).attr("href"));
})
var repeat = false;

function readMP4(){

$("#jquery_jplayer_1").jPlayer("destroy");
$("#jquery_jplayer_1").jPlayer({
ready: function () {
//document.writeln("aun vivo");
var data = $.ajax({
type:'POST',
url: "getvideo.php",
data: {'myval': 16 },
async: false
});
var string = data.split('|');
var addToPlay = 0;
for (var x = 0; x<(string.length - 2); x++){
//document.writeln("aun vivo" + string[x]);
if (x % 2 == 0){
playlist.add({
m4v: string[x],
title: string[(x+1)]
});
addToPlay++;
}
}
repeat = true;

},
ended: function (event) {
if (current_clicked_item.index() < $(".jp-play1").length - 1)
{
$(".jp-play1").eq(current_clicked_item.index() + 1).trigger('click'); // play next song
}
else
{
$(".jp-play1").eq(0).trigger('click'); // play first song
}
},
swfPath: "../../../dist/jplayer",
supplied: "webmv, ogv, m4v",
useStateClassSkin: true,
autoBlur: false,
smoothPlayBar: true,
keyEnabled: true
});

}

});
//]]>
</script>
@stop


getvideo.php

<?php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){

mysql_connect("localhost", "root", "root") or die("Error connecting to database: ".mysql_error());
mysql_select_db("contenidoAudiovisual") or die(mysql_error());

$myval = $_POST['myval'];
$myval1 = htmlspecialchars($myval);

$raw_results = mysql_query("SELECT * FROM movie_in_playlist WHERE (`playlist_id` LIKE '%".$myval1."%') " )) or die(mysql_error());
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
$songname = $results['name'];
$url = '../files/convert/videos/'.$results['url'];
$separator = '|';
echo $url.$separator.$songname.$separator;

}
}

?>


The strangest thing is that it was working, I don't know what happened but now i have this error.

Answer

OK, This was the solution. I change the ajax function for this:

$(document).ready(function(){
playlist = new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_1",
    cssSelectorAncestor: "#jp_container_1"
  });
var current_clicked_item = $(".jp-play1").eq(0);
readMP4();

var repeat = false;

function readMP4(){

  $("#jquery_jplayer_1").jPlayer("destroy");
  $("#jquery_jplayer_1").jPlayer({
    ready: function () {
      if(repeat == false){
        var data = $.post('../php/getvideo.php',{
          id:{{$playlist->id}}

        },function(data,status){
            var string = data.split('|');
            var addToPlay = 0;
            for (var x = 0; x < (string.length-1); x++){
              if (x % 2 == 0){
                playlist.add({
                  m4v: string[x],
                  title: string[(x+1)]
                });
                addToPlay++;
              }
            }
        });
      repeat = true;
      }
    },
    swfPath: "../../../dist/jplayer",
    supplied: "webmv, ogv, m4v",
    useStateClassSkin: true,
    autoBlur: false,
    smoothPlayBar: true,
    keyEnabled: true
  });

}

});

And now the function is working.