我有以下Json:
[{"label":"75001","value":"75001"},
{"label":"75002","value":"75002"},
{"label":"75003","value":"75003"},
{"label":"75004","value":"75004"},
{"label":"75005","value":"75005"},
{"label":"75006","value":"75006"},
{"label":"75007","value":"75007"},
{"label":"75008","value":"75008"},
{"label":"75009","value":"75009"}]
它会在 JQuery 中导致解析错误。
我实际上使用 jquery.ui 自动完成控件如下:
jQuery(document).ready(function() {
jQuery("#accountPostcode").autocomplete({
source : function(request, response) {
var jqxhr = jQuery.ajax({
url : "utils/JSonPostcodesWithQueryParam",
dataType : "json"
}).fail(function() {
console.log("error:");
console.log(jqxhr.statusText);
});
},
minLength : 2
});
});
我不确定我做错了什么,因为我的 Json 似乎是正确的......
任何人有任何线索?
编辑:
以下是生成 Json 的内容:
package com.bignibou.web.pages.utils;
import java.util.List;
import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.RequestParameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.TextStreamResponse;
import com.bignibou.domain.utils.PostcodeJson;
import com.bignibou.service.AccountService;
import com.google.gson.Gson;
public class JSonPostcodesWithQueryParam {
@Inject
private AccountService service;
@OnEvent(EventConstants.ACTIVATE)
StreamResponse loadPostcodes(@RequestParameter(value = "term") String beginningOfPostcode) {
Gson gson = new Gson();
List<PostcodeJson> postcodes = service.loadPostcodes(beginningOfPostcode);
return new TextStreamResponse("application/json","UTF-8", gson.toJson(postcodes));
}
}