[英]JQuery Auto complete and webapi 2
我正在使用WebApi嘗試使json arry進入我網站中的自動填充jQuery,
我簡單地需要將一個列表修剪成一個自動完成的對象,以便向客戶顯示模板列表,
這是我的Javascript代碼-
<script>
var availableTags;
$.get(
"http://localhost:59040/api/Email/GetTemplates",
{ 'FirstName': "what she say", '': "sayhey" }, // put your parameters here
function (responseText) {
availableTags = responseText;
console.log(availableTags);
DoIt();
}
);
function DoIt(){
$(function () {
var ava = availableTags;
if (availableTags){
console.log("Its done")
$("#TemplatesAuto").autocomplete({
source: availableTags
});
}
else {
console.log("Not");
}
});
};
</script>
還有我的webapi控制器代碼:
//Get templates
[HttpGet]//omUrl/{url?}
[Route(@"~/api/Email/GetTemplates")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public string GetTemplates()
{
List<string> Names = new List<string>();
Names.Add("Check");
Names.Add("Check");
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(Names);
}
似乎正常,工作正常。 但是,當我嘗試在自動完成輸入中寫一些東西時,出現此錯誤:
GET http://localhost:54662/Check/[%22Check%22,%22Check%22]?term=c 404 (Not Found)
我在這里瘋了,來這里之前要花幾個小時尋找答案,
請幫忙。
非常感謝,美好的一天:)
我找到了!..
花了一點時間來說明我沒有將其設置為我的JavaScript的對象,
我要做的就是做
List<string> Names = new List<string>();
Names.Add("Check");
Names.Add("Check");
JavaScriptSerializer serializer = new JavaScriptSerializer();
string g = serializer.Serialize(Names);
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(g, Encoding.UTF8, "application/json");
return response;
這樣就行了,
非常感謝您的嘗試,美好的一天。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.