2013-01-02 50 views
0

我想使用jquery自動完成功能。數據來自以json格式返回數據的web服務。但關聯的texbox中的輸出不起作用。我顯示了完整的json數組。jQuery與web服務,ajax和json的自動完成不會返回正確的值

請給我一個提示。

我的WebService的樣子:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 
using System.Data.SqlClient; 
using MySql.Data; 
using MySql.Data.MySqlClient; 
using System.Data; 
using System.Configuration; 

namespace WebApplication1 
{ 
/// <summary> 
/// Zusammenfassungsbeschreibung für WebService1 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 

[System.Web.Script.Services.ScriptService] 

public class WebService1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string customer(string queryParam) 
    { 



string conStr= 
ConfigurationManager.ConnectionStrings["mySql_classicmodels"].ConnectionString; 

     MySqlConnection con = new MySqlConnection(conStr); 
     MySqlCommand readCommand = new MySqlCommand("SELECT customerName, contactLastName, contactFirstName, phone FROM customers where customerName like '%" + queryParam + "%'", con); 
     MySqlDataAdapter adapter = new MySqlDataAdapter(readCommand); 
     DataTable datatable = new DataTable(); 

     adapter.Fill(datatable); 

     return DataTableToJSON(datatable); 




    } 

public static string DataTableToJSON(DataTable table) 
{ 
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); 

foreach (DataRow row in table.Rows) 
{ 
    Dictionary<string, object> dict = new Dictionary<string, object>(); 

    foreach (DataColumn col in table.Columns) 
    { 
     dict[col.ColumnName] = row[col]; 
    } 
    list.Add(dict); 
} 
JavaScriptSerializer serializer = new JavaScriptSerializer(); 
return serializer.Serialize(list); 
} 


} 


} 

我的ASP/jQuery代碼看起來像:

$(document).ready(function() { 
     $("#auto").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        type: "POST", 
        url: "Webservice1.asmx/customer", 
        dataType: "json", 
        data: '{ queryParam: "' + request.term + '" }', 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         response($.map(data, function (item) { 
          return { 
           value:item 

          } 
         })); 
        }, 
        minLength: 2, 
        error: function (xhr, ajaxOptions, thrownError) { 
         alert(xhr.status); 
         alert(thrownError); 
         alert(xhr.responseText); 
        } 
       }); 
      } 
     }) 
    }) 



</script>  

我的結果是這樣的:

[{"customerName":"Australian Collectors, Co.","contactLastName":"Ferguson","contactFirstName":"Peter","phone":"03 9520 4555"},{"customerName":"Australian Gift Network, Co","contactLastName":"Calaghan","contactFirstName":"Ben","phone":"61-7-3844-6555"},{"customerName":"Australian Collectables, Ltd","contactLastName":"Clenahan","contactFirstName":"Sean","phone":"61-9-3844-6555"}] 

What's回事?

回答

0

因爲您將整個對象設置爲一個值。我相信這應該是一個字符串。嘗試:

return { value: item.customerName }; 
+0

我已經嘗試過您的提示,但它沒有奏效。現在相關的文本框不會給任何回報。 – user1944111