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

ajax cooperates with jsonp to solve cross-domain access problems

巴扎黑
Release: 2016-11-25 11:05:35
Original
1261 people have browsed it

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();

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template