NetworkError:当我在laravel中使用ajax调用php文件时出现500内部服务器错误

时间:2016-09-02 21:51:50

标签: jquery ajax laravel

当我在我的laravel项目中将一个php文件调用到ajax jquery函数时,我遇到了一个问题,我收到一个错误: POST http://localhost:8000/getvideo.php 500内部服务器错误 " NetworkError:500内部服务器错误 - http://localhost:8000/getvideo.php"

这是观点:

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

  }
} 

?> 

最奇怪的是它有效,我不知道发生了什么,但现在我有这个错误。

2 个答案:

答案 0 :(得分:2)

好的,这是解决方案。我更改了ajax函数:

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

}

});

现在功能正常。

答案 1 :(得分:0)

为什么要尝试在 Laravel 项目中调用php file

应该像http://localhost:8000/getvideo

记得将getvideo添加到route.php 这就是我用jquery获取输入的方法。

Route::get('/getvideo',function(){
   $ID = Input::get('you id'); //here if you want to get any input
   $IDs = DB::table('table1')->where('ID','=',$ID)->get(); //DB query you want
    return Response::json($IDs); //when you return to ajax, you must use this way.
});