使用自定义控件在json中从WCF获取员工。以下是代码
$("#" + txtEmployeeInput).tokenInput(urlService + '/GetEmployeesForTokenInput', {
prePopulate: selectedEmployeesForTokenInput,
preventDuplicates: true,
tokenLimit: 1,
hintText: "Type to select employees",
noResultsText: "No employees found!",
searchingText: "searching in employees...",
onAdd: function (item) {
},
onDelete: function (item) {
},
theme: "facebook"
});
现在这项服务
urlService + '/GetEmployeesForTokenInput'
以下列格式返回员工(JSON数组字符串)
"[{\"id\":\"4173\",\"name\":\"TAHA SIDDIQUI\"},{\"id\":\"a1234\",\"name\":\"TAHA REHMAN SIDDIQUI\"},{\"id\":\"70007\",\"name\":\"TAHA SANDAM SIDDIQUI\"},{\"id\":\"57880\",\"name\":\"TAHA SANDAM SIDDIQUI\"},{\"id\":\"4333\",\"name\":\"TAHA HASAN\"},{\"id\":\"555-12\",\"name\":\"TAHA REHMAN SIDDIQUI\"},{\"id\":\"49200\",\"name\":\"TAHA -\"},{\"id\":\"1645\",\"name\":\"TAHAN A RICE\"},{\"id\":\"5253\",\"name\":\"TAHFAREN LAWRENCE OWENS\"},{\"id\":\"57881\",\"name\":\"Tahir Ali\"},{\"id\":\"563r44\",\"name\":\"TAHIR MALIK\"},{\"id\":\"52612\",\"name\":\"TAHIR MUJAHID\"},{\"id\":\"34115\",\"name\":\"TAHIRA -\"},{\"id\":\"18665\",\"name\":\"TAHIRA AQUIB HUSSAIN\"}]"
现在,呼叫正在成功,下面的屏幕截图几乎可以解释我所面临的问题。
下拉列表未显示。现在我怀疑是因为结果是JSON而不是javascript数组,现在如果这是一个问题,任何人都可以建议我在哪里将字符串转换为代码中的数组。
WCF代码
[OperationContract]
[WebGet(UriTemplate = "GetEmployeesForTokenInput?q={value}",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
string GetEmployeesForTokenInput(string value);
public object GetEmployeesForTokenInput(string value)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Methods", "GET"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Headers", "Content-Type, Accept");
Employee employee = new Employee();
employee.FullName = value;
EmployeeManagement empMgmt = new EmployeeManagement();
int temp;
List<Employee> employees = empMgmt.Search(employee, 0, 0, out temp);
var jsonData =
from row in employees
select new
{
id = row.TRGEmpID,
name = row.First_Name + " " + row.Middle_Initial + " " + row.Last_Name
};
return (new JavaScriptSerializer()).Serialize(jsonData);
}
答案 0 :(得分:0)
当我尝试通过JSON字符串运行时,插件实际上需要一个具有属性id和name的对象Array。所以我修改了
function populate_dropdown(query, results)
并在函数启动时添加了这个
results = $.parseJSON(results);