处理从 WCF 以 json 格式获取员工的自定义控件。下面是代码
$("#" + 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);
}