所以我试图使用Select2插件和AJAX进行自动完成,从jsp文件发送和接收(Json)数据。所以我的问题是,当调用AJAX并将数据发送到jsp文件时,我检查了jsp文件是否完美运行我收到此消息 无法加载结果。
这是AJAX的jquery代码
$("#id").select2( {
placeholder: 'Select an item',
ajax: {
url: 'autocomplete.jsp?type=dbpedia_resources',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
之前我使用的是同样的结果
$("#entities").select2( {
minimumInputLength: 2,
tags: [],
ajax: {
url: "autocomplete.jsp?type=dbpedia_resources",
dataType: 'json',
type: "GET",
delay: 250,
data: function (term) {
return {
q: term
};
},
results: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
//slug: item.slug,
id: item.id
}
})
};
}
}
});
jsp / java代码就是这个
<%@page import="java.util.Map"%>
<%@page contentType="application/json" pageEncoding="UTF-8"%>
<%@page import="com.aylien.newsapi.*"%>
<%@page import="com.aylien.newsapi.auth.*"%>
<%@page import="com.aylien.newsapi.models.*"%>
<%@page import="com.aylien.newsapi.parameters.*"%>\
<%@page import="org.json.*"%>
<%@page import="com.aylien.newsapi.api.DefaultApi"%>
<%
String type = request.getParameter("type");
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: app_id
ApiKeyAuth app_id = (ApiKeyAuth) defaultClient.getAuthentication("app_id");
app_id.setApiKey("");
// Configure API key authorization: app_key
ApiKeyAuth app_key = (ApiKeyAuth) defaultClient.getAuthentication("app_key");
app_key.setApiKey("");
DefaultApi apiInstance = new DefaultApi();
//String type = "dbpedia_resources";
String term = request.getParameter("q");
String language = "en";
Integer perPage = 10;
AutocompletesParams.Builder autocompletesBuilder = AutocompletesParams.newBuilder();
autocompletesBuilder.setType(type);
autocompletesBuilder.setTerm(term);
autocompletesBuilder.setLanguage(language);
autocompletesBuilder.setPerPage(perPage);
try {
Autocompletes result = apiInstance.listAutocompletes(autocompletesBuilder.build());
JSONObject jsonObject = new JSONObject(result);
out.println(jsonObject.toString());
System.out.println(result.getAutocompletes());
} catch (ApiException e) {
System.err.println("Exception when calling DefaultApi#listAutocompletes");
e.printStackTrace();
}
%>
我做错了什么?
提前谢谢。