I have asp.net mvc 4 project where try to implement autocomplete but have an error: unexpected token u. I know that this mean that I have unidentified object but dont understand what I should to do. Anybody can help me?
<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);
}
And here is my responce:
"[\"utyutu\",\"qweqweqweqweqwe\",\"йцуйцуqweqeasdzxc\",\"zxc\",\"dfg\",\"vbn\",\"mbm\",\"hjgkjgйцуйу\",\"йцуцй\",\"йцу\",\"йцуйцуйцуфыв\",\"ячсмям\",\"йцуйцуцу\",\"йцуйцуцу\",\"йцуйцуцу\",\"йцуйцуцу\"]"
I think your data is getting serialized twice.. Once by the JavaScriptSerializer
, and once in the Json()
method. Try this:
public JsonResult GetListofNames() {
var companies = _equipmentRepo.GetAll().Select(x => x.Name).ToList();
return Json(companies, JsonRequestBehavior.AllowGet);
}
Try this example, you are serializing it and it not required.
a link !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.