Front page:
$.ajax({
type:'GET',
url:url,
async:false,
jsonpCallback:'jsonCallback',
contentType:"application/json",
dataType:"jsonp",
success:function(json){
alert(json);
},
error:function(e){
console.log(e.message);
}
});
后台Servlet:
JSONObjectjsonObject=newJSONObject();
jsonObject.put("msg","hello");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
response.getWriter().write(jsonObject.toString());
response.getWriter().flush();
However, when the foreground accepts the value, it throws “syntax error: missing” through firebug debugging; After searching Baidu for a long time, I didn’t find a reliable method. Many posts said it was URL problem, but I’m sure there was no problem with URL. Later, I switched to Google and found out the root of the problem. Hey… The gap, I still recommend Google
Reference website:
http://stackoverflow.com/questions/20658674/syntaxerror-missing-before-statement-jquery-jsonp
Solution:
Just remove the attribute datatype: “jsonp”. I haven’t checked the specific reason yet. I’m off duty. I’ll continue to get to the bottom tomorrow
$.ajax({
type:'GET',
url:url,
contentType:"application/json",
success:function(json){
alert(json);
},
error:function(e){
console.log(e.message);
}});