2013-11-22 75 views
0

我在我的ajax調用中收到內部服務器錯誤500。我使用LAMP堆棧作爲我的服務器,並且已經檢查了權限,因此不能是我的錯誤。Jquery和Ajax內部服務器錯誤500

這裏是我的javascript代碼:

//$('#addAthleteForm').trigger("reset"); 

//$(document).ready(function() { 

$(function() { 
    $("#dialog").dialog({ 
     autoOpen: false, 
     maxWidth: 600, 
     maxHeight: 500, 
     width: 500, 
     height: 460, 
     close: function() { 
      $('#addAthleteForm').trigger("reset"); 
     } 

    }); 
    $("#addAthlete").on("click", function() { 
     $("#dialog").dialog("open"); 
    }); 

    $("#addAthleteForm").submit(function (e) { 
     e.preventDefault(); 
     var postData = jQuery(this).serialize(); 
     $("#dialog").dialog("close") 
     $.ajax({ 
      type: "POST", 
      url: "AddAthletes.php", 
      dataType: 'json', 
      data: postData, 
      success: function (data) { 
       alert(data); 
      }, 
      error: function (jqXHR, exception) { 
       if (jqXHR.status === 0) { 
        alert('Not connect.\n Verify Network.'); 
       } else if (jqXHR.status == 404) { 
        alert('Requested page not found. [404]'); 
       } else if (jqXHR.status == 500) { 
        alert('Internal Server Error [500].'); 
       } else if (exception === 'parsererror') { 
        alert('Requested JSON parse failed.'); 
       } else if (exception === 'timeout') { 
        alert('Time out error.'); 
       } else if (exception === 'abort') { 
        alert('Ajax request aborted.'); 
       } else { 
        alert('Uncaught Error.\n' + jqXHR.responseText); 
       } 
      } 



     }); 
    }); 


    $("#editAthlete").submit(function (e) { 
     e.preventDefault(); 
     var editData = jQuery(this).serialize(); 
     $.ajax({ 
      type: "POST", 
      url: "GetAthlete.php", 
      dataType: 'json', 
      data: editData, 
      success: function (data) { 

       var form = document.forms['addAthleteForm']; 
       form.fname.value = data.fname; 
       form.lname.value = data.lname; 
       form.school.value = data.school; 
       form.agegrp.value = data.agegrp; 
      } 

     }); 

     $("#dialog").dialog("open"); 

    }); 
}) 

這是我的PHP代碼:

<?php 
$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$school = $_POST['school']; 
$agegrp = $_POST['agegrp']; 


$db = mysqli_connect("localhost", "root", "passwrd","site"); 
    if(!$db){ 
     exit("Error in database connection"); 
    } 

else{ 

$result = mysqli_query($db, "SELECT * FROM `School` WHERE `SchoolLong`='$school'"); 
$row = mysqli_fetch_array($result)); 
$SchoolID = $row['SchoolID']; 

$result = mysqli_query($db,"SELECT * FROM `AgeGroup` WHERE `AgeGroupLong`='$agegrp'"); 
$row = mysqli_fetch_array($result)); 
$AgeGroupID = $row['AgeGroupID']; 


mysqli_query($db, "INSERT INTO `Athlete` (`NameFirst`,`NameLast`, `SchoolID`, `AgeGroupID`) VALUES ('$fname', '$lname', $schoolID, $agegrpID)"); 


} 

echo json_encode($fname); 
?> 

我不知道在哪裏這個錯誤的來源。代碼用於工作。

+3

檢查發球r錯誤日誌以獲取關於500的更多詳細信息。 –

+0

轉到您的PHP頁面(不通過ajax調用)並確認其響應如預期。 – Moob

回答

2

語法錯誤在下面一行: -

$row = mysqli_fetch_array($result)); 

它應該是: -

$row = mysqli_fetch_array($result); 

下面mysqli_fetch_array()

你的代碼修正塊同樣的錯誤是: -

$result = mysqli_query($db, "SELECT * FROM `School` WHERE `SchoolLong`='$school'"); 
$row = mysqli_fetch_array($result); 
$SchoolID = $row['SchoolID']; 

$result = mysqli_query($db,"SELECT * FROM `AgeGroup` WHERE `AgeGroupLong`='$agegrp'"); 
$row = mysqli_fetch_array($result); 
$AgeGroupID = $row['AgeGroupID']; 
+0

這將做到這一點。 – Moob

+0

很好的結果,但作爲'工作的代碼',這可能不是原因。 – jeroen

+0

我很確定這是唯一的問題:) – Roopendra

相關問題