我有一个REST API控制器配置如下。在按钮上单击“PlayersRegistration”,javascript中的更新功能称为prorperly。但问题是DefaultController类中的PostPlayers方法永远不会使用success:function(data)语句在update函数中调用。相反,当我通过检查元素检查浏览器中的控制台时,我看到内部服务器错误500.我不知道什么是错的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApplication2.Models
{
public class Default : ApiController
{
public string regID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string teamName { get; set; }
public DateTime dateOfBirth { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication2.Models;
using System.IO;
using System.Globalization;
using System.Windows.Forms;
namespace WebApplication2.Controllers
{
public class DefaultController : ApiController
{
List<Default>players = new List<Default>();
string path = Path.Combine(Directory.GetCurrentDirectory(), "\\players.txt");
public void readFile()
{
string line;
StreamReader file = new StreamReader(path);
string[] data;
players.Clear();
while ((line = file.ReadLine()) != null)
{
data = line.Split(',');
Default player = new Default();
player.regID = data[0];
player.firstName = data[1];
player.lastName = data[2];
player.teamName = data[3];
player.dateOfBirth = DateTime.ParseExact(data[4],
"yyyy-mm-dd", CultureInfo.InvariantCulture);
players.Add(player);
}
file.Close();
}
public IHttpActionResult PostPlayers(Default playerInfo)
{
readFile();
MessageBox.Show("5");
var player = players.FirstOrDefault((p) => p.regID == playerInfo.regID);
if (player != null)
{
players.Remove(player);
players.Add(playerInfo);
}
else
{
players.Add(playerInfo);
}
using (StreamWriter sw = File.CreateText(path))
{
for (int i = 0; i < players.Count(); i++)
sw.WriteLine(Players_Display(players[i]), Environment.NewLine);
sw.Close();
}
return Ok(players);
}
public string Players_Display(Default player)
{
string data = player.regID + "," + player.firstName + "," + player.lastName + "," + player.teamName + "," + player.dateOfBirth.ToString("yyyy-mm-dd").Substring(0,10);
return data;
}
}
}
HTML代码如下
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div>
<h2> All Players</h2>
<ul id="players"></ul>
</div>
<div>
<br />
<h2> Search or Delete </h2>
<select id="options_for_search_delete">
<option value="id">ID</option>
<option value="name">Name</option>
</select>
<input type="text" id="data" size="5" />
<input type="button" value="Search" onclick="search()" />
<input type="button" value="Delete" onclick="delet()" />
<br />
<p id="result_players" /><br>
<h2>Player Registration</h2>
<br />
<form>
<label for="id"> RegistrationID:</label><br>
<input type="text" id="regID" name="regID">
<br />
<label for="firstName">First Name:</label><br>
<input type="text" id="firstName" name="firstName">
<br />
<label for="lastName">Last Name:</label><br>
<input type="text" id="lastName" name="lastName">
<br />
<label for="teamName">Team Name:</label><br>
<input type="text" id="teamName" name="teamName">
<br />
<label for="dateofbirth">Date Of Birth:</label><br>
<input type="text" id="dateOfBirth" name="dateOfBirth">
<br />
<input type="button" id="post" value="Player Registration" onclick="update();" />
<p id="data_validation"></p>
</form>
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
var uri = 'api/Default';
$(document).ready(function () {
$.ajax({
type: 'GET',
url: uri,
success: function (data) {
displayData(data);
}
})
})
function format(item) {
return item.regID + "," + item.firstName + " " + item.lastName + "," + item.teamName + "," + item.dateOfBirth.toString().substring(0, 10);
}
function displayData(data) {
$('ul').empty();
$.each(data, function (key, item) {
$('<li>', { text: format(item) }).appendTo($('#players'));
});
}
function update() {
$('#result_players').text("");
$('#data_validation').text("");
if ($('#regID').val() == "" || $('#firstName').val() == "" || $('#lastName').val() == "" || $('#teamName').val() == "" || $('#dateofbirth').val() == "") {
$('#data_validation').text("Input Boxes Cannot be Empty");
}
else {
$.ajax({
type: 'POST',
url: uri,
data: String($('form').serialize()),
success: function (data) {
displayData(data);
},
error: function (jqXHR, textStatus, err) {
alert(err);
}
})
}
}
</script>
</body>
</html>
答案 0 :(得分:0)
“500内部服务器错误”表示“服务器遇到意外情况,导致无法完成请求。”
所以,你的javascript可能很好,但服务器端无法执行请求,并且永远不会调用成功回调。
您必须检查服务器的日志,以获取有关根本原因的线索。