jQuery jsopドメインを越えてパッケージ機能を要求します。
1671 ワード
以下のようにjsonpドメイン横断要求パッケージのajaxコードを示します。
詳しく説明してみます。https://blog.csdn.net/guanmao4322/article/details/88782995
参考:https://www.cnblogs.com/chiangchou/p/jsonp.htmlありがとうございます。
詳しく説明してみます。https://blog.csdn.net/guanmao4322/article/details/88782995
$(document).ready(function () {
$("#btn").click(function () {
$.ajax({
url: "http://localhost:9090/student",//
type: "GET",
dataType: "jsonp", //
jsonp: "callback", //
jsonpCallback: "showData", //
});
}
});
//
function showData (data) {
console.info(" showData");
var result = JSON.stringify(data);
$("#text").val(result);
}
サーバ端:protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// *
response.setHeader("Access-Control-Allow-Origin", "*");
//
//response.setHeader("Access-Control-Allow-Origin", "http:localhost:8080/");
//
List studentList = getStudentList();
JSONArray jsonArray = JSONArray.fromObject(studentList);
String result = jsonArray.toString();
//
String callback = request.getParameter("callback");
// , ,
result = callback + "(" + result + ")";
response.getWriter().write(result);
}
注:HTMLページではjQuery.jsの導入に注意してください。参考:https://www.cnblogs.com/chiangchou/p/jsonp.htmlありがとうございます。