2013-06-18 58 views
0

試圖獲得測試平臺爲jQuery UI V10自動完成工作。我有演示程序工作,但是當我使用C#和ASP.NET webservice將代碼調整爲示例時,我得到的狀態爲200 parseerror。 jSon數據重新包裝在雙引號中。當我將響應放入jsonLint中時,它將驗證我何時刪除外部引號。它也有「d」。前綴,我期望從ASP Web服務。jQuery自動完成小部件在調用C#webservice時會得到ParseError

如何獲得響應以返回有效的JSON?

jQuery代碼:

$(settings.selector).autocomplete({ 
    source: function (request, response) { 
     var data = JSON.stringify({ 
      prefixText: request.term, 
      count: settings.count 
     }); 
     ajaxCall(settings.url, data, { 
      onSuccess: function (data) { 
       var matches = []; 
       $.each(data, function (item) { 
        matches.push({ 
         label: data[item].label, 
         value: data[item].value, 
         id: data[item].id 
        }); 
       }); 
       response(matches); 
      } 
     }); 
    }, 
    minLength: settings.minLength, 
    change: function (event, ui) { 
     if (ui && ui !== null && ui.item && ui.item !== null) { 
      currentValue = ui.item.label; 
      if (settings.onChange && settings.onChange !== null) { 
       settings.onChange(ui.item, settings.selector, oldValue); 
      } 
     } else { 
      $(settings.selector).val(oldValue); 
     } 
    }, 
    select: function (event, ui) { 
     currentValue = ui.item.label; 
     if (settings.onSelect && settings.onSelect !== null) { 
      settings.onSelect(ui.item, settings.selector, oldValue); 
     } 
    }, 
    open: function() { 
     $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
    }, 
    close: function() { 
     $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
    } 
}); 

WebService的代碼:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 

[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] 
public class TestAutocomplete : System.Web.Services.WebService 
{ 

    [WebMethod(BufferResponse = true, Description = "Lookup City")] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string CityList(string prefixText, int count) 
    { 
     List<AutoSuggestReturn> matches = AssessorBusinessLayer.City.List(prefixText, count); 
     Messages.ConsoleLog(string.Format("Prefix: {0} return {1} matches", prefixText, matches.Count)); 
     string ret = AutoSuggestReturn.ConvertToJson(matches); 
     return ret; 
    } 
} 

XHR對象從Web服務調用返回:

xhr: Object 
abort: function (statusText) { 
always: function() { 
complete: function() { 
done: function() { 
error: function() { 
fail: function() { 
getAllResponseHeaders: function() { 
getResponseHeader: function (key) { 
overrideMimeType: function (type) { 
pipe: function (/* fnDone, fnFail, fnProgress */) { 
progress: function() { 
promise: function (obj) { 
readyState: 4 
responseText: "{"d":"[{\"id\":\"10499\",\"label\":\"Delmar,AL (35551)\",\"value\":\"Delmar,AL (35551)\"},{\"id\":\"2679\",\"label\":\"Delmar,DE (19940)\",\"value\":\"Delmar,DE (19940)\"},{\"id\":\"2401\",\"label\":\"Delmar,IA (52037)\",\"value\":\"Delmar,IA (52037)\"},{\"id\":\"2679\",\"label\":\"Delmar,MD (21875)\",\"value\":\"Delmar,MD (21875)\"},{\"id\":\"3584\",\"label\":\"Delmar,NY (12054)\",\"value\":\"Delmar,NY (12054)\"},{\"id\":\"4780\",\"label\":\"Delmita,TX (78536)\",\"value\":\"Delmita,TX (78536)\"},{\"id\":\"3352\",\"label\":\"Delmont,NJ (08314)\",\"value\":\"Delmont,NJ (08314)\"},{\"id\":\"11550\",\"label\":\"Delmont,PA (15626)\",\"value\":\"Delmont,PA (15626)\"},{\"id\":\"4574\",\"label\":\"Delmont,SD (57330)\",\"value\":\"Delmont,SD (57330)\"}]"}" 
setRequestHeader: function (name, value) { 
state: function() { 
status: 200 
statusCode: function (map) { 
statusText: "OK" 

回答

1

什麼是AutoSuggestReturn.ConvertToJson

自動完成正在尋找某種類型的json數組,並且您應該能夠返回您的List<AutoSuggestReturn> matches枚舉而不必進行字符串轉換。序列化將爲您處理。

相關問題