Home  >  Article  >  Web Front-end  >  ajax cooperates with jsonp to solve cross-domain access problems

ajax cooperates with jsonp to solve cross-domain access problems

巴扎黑
巴扎黑Original
2016-11-25 11:05:351201browse

Front end:
$.ajax({
type: "get",
async: false,//Whether it is asynchronous
url: "http://ip:port/servlet address",
dataType: "jsonp",
contentType : "application/x-www-form-urlencoded;charset=UTF-8",
jsonpCallback: "callback",
data: { }, //Parameters passed
success: function(e){
var r = $ .parseJSON(e);//e: return value
},
error: function(XMLHttpRequest, textStatus, errorThrown) {}
});
Backend: example using servlet
String callback = request.getParameter(" callback");//Callback function
request.setCharacterEncoding("utf-8");//Prevent Chinese garbled characters
response.setCharacterEncoding("utf-8");//Prevent Chinese garbled characters
//response.setHeader(" pragma", "no-cache");
//response.setHeader("cache-control", "no-cache");
//list the results to be returned
JSONArray jsonArray = JSONArray.fromObject(list);
response.getWriter().print(callback+"('"+jsonArray+"')");
response.getWriter().flush();

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn