这是我的视图页面,其中包含我的脚本,这是我声明我的自动完成脚本和我的脚本,我从Web服务获取信息
@model IEnumerable<BarclaysApp.Models.Stadiums>
@section Scripts
{
<script src="~/jquery-ui.js"></script>
<link href="~/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
$(function () {
$("#SearchString").autocomplete({
source: '@Url.Action("AutocompleteSuggestions")',
minLength: 1,
select: function (event, ui) {
if (ui.item) {
$("#SearchString").val(ui.item.value);
$("#ResultsDiv").submit();
}
}
});
});
function DoSearch() {
$("#resultsDiv").html("");
var key = { "key": $("#searchString").val() };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Stadium.asmx/GetStadiumByName",
data: JSON.stringify(key),
dataType: "json",
async: true,
success: function (result, textStatus) {
if (textStatus == "success") {
for (var i = 0; i < result.d.length; i++) {
$("#resultsDiv").html($("#resultsDiv").html() + result.d[i] + "<br/>");
}
}
},
error: function (result, status, error) {
$("#resultsDiv").html("Error: " + error + " <br/>")
}
});
}
</script>
}
@{
ViewBag.Title = "Index";
}
<h2>Stadiums</h2>
<div class="jumbotron">
<div>
<input id="SearchString" type="text" placeholder="Stadium" />
<input type="button" value="Search" onclick="DoSearch();" />
</div>
<div id="resultsDiv"></div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Capacity)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Team)
</th>
<th>
@Html.DisplayNameFor(model => model.Location)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Capacity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Team)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.stadiumID }) |
@Html.ActionLink("Details", "Details", new { id = item.stadiumID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.stadiumID })
</td>
</tr>
}
</table>
</div>
这是我的webservice文件,这是我的get方法用于显示用户搜索的体育场
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BarclaysApp.Models;
namespace BarclaysApp
{
/// <summary>
/// Summary description for Stadium
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Stadium : System.Web.Services.WebService
{
private ApplicationDbContext db = new ApplicationDbContext();
[WebMethod]
public string[] GetStadiumsByName(string term)
{
List<string> stadiumlist = new List<string>();
if (term != "")
{
foreach (var Name in db.Stadiums
.Where(a => a.Name.Contains(term))
.ToList())
{
stadiumlist.Add(Name.Name);
}
}
return stadiumlist.ToArray();
}
}
}
我的自动完成工作正常,但当我去搜索时,我得到错误:内部服务器错误500
答案 0 :(得分:0)
尝试将AJAX请求的类型更改为GET。好的做法是建议GET用于读取数据,POST用于将信息(写入操作)发送到服务器。