简体   繁体   English

jQuery自动完成和webapi 2

[英]JQuery Auto complete and webapi 2

I am using WebApi to try and get json arry to my autocomplete jquery in my website, 我正在使用WebApi尝试使json arry进入我网站中的自动填充jQuery,

I simpley need to trun a List into a auto complete object so i can show my clients a list of templates, 我简单地需要将一个列表修剪成一个自动完成的对象,以便向客户显示模板列表,

This is my Javascript Code - 这是我的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>

And my webapi Controller Code : 还有我的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);   
        }

Seems normal, works. 似乎正常,工作正常。 BUT, when i try to write something in my autocomplete input, i get this error: 但是,当我尝试在自动完成输入中写一些东西时,出现此错误:

GET http://localhost:54662/Check/[%22Check%22,%22Check%22]?term=c 404 (Not Found)

I am getting crazy out here, looking for hours for the answer before coming here, 我在这里疯了,来这里之前要花几个小时寻找答案,

Please help. 请帮忙。

Thanks alot, Good day :) 非常感谢,美好的一天:)

i found it!.. 我找到了!..

Took me a time to relize that i was not seting it as an object to my javascript, 花了一点时间来说明我没有将其设置为我的JavaScript的对象,

All i needed to do is to do 我要做的就是做

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;

Like that and it works, 这样就行了,

Thanks alot for trying , good day. 非常感谢您的尝试,美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM