我有asp.net mvc 4项目,试图实现自动完成但有错误:意外的令牌你。我知道这意味着我有一个不明物体,但不明白我应该做什么。有人可以帮帮我吗?
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
@using (Html.BeginForm("FilterEquipment", "Home", FormMethod.Get)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-field" style="float: left; padding-top: 2px; padding-right: 10px;">
@Html.EditorFor(model => model.SearchQuery)
@Html.ValidationMessageFor(model => model.SearchQuery)
</div>
<div class="editor-field">
<input type="submit" value="Search" />
</div>
</fieldset>
}
<script>
request();
function request() {
$.ajax({
url: '@Url.Action("GetListofNames", "Home")',
type: "GET",
dataType: "json",
contentType: 'application/json',
data: {}
}).success(function (data) {
var responseArray = JSON.parse(data.d);//here is an error
$("#SearchQuery").autocomplete({
source: responseArray
});
})
.fail(function (e) {
alert("Error");
});
}
</script>
public JsonResult GetListofNames() {
var companies = _equipmentRepo.GetAll().Select(x => x.Name).ToList();
var oJss = new JavaScriptSerializer();
String strResponse = oJss.Serialize(companies);
return Json(strResponse, JsonRequestBehavior.AllowGet);
}
这是我的回答:
"[\"utyutu\",\"qweqweqweqweqwe\",\"йцуйцуqweqeasdzxc\",\"zxc\",\"dfg\",\"vbn\",\"mbm\",\"hjgkjgйцуйу\",\"йцуцй\",\"йцу\",\"йцуйцуйцуфыв\",\"ячсмям\",\"йцуйцуцу\",\"йцуйцуцу\",\"йцуйцуцу\",\"йцуйцуцу\"]"
答案 0 :(得分:0)
我认为您的数据被序列化了两次..一次是JavaScriptSerializer
,一次是Json()
方法。试试这个:
public JsonResult GetListofNames() {
var companies = _equipmentRepo.GetAll().Select(x => x.Name).ToList();
return Json(companies, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
尝试此示例,您正在对其进行序列化,而不是必需的。