In front of the use of Ajax, encountered a problem: the interface developed to use on the phone, but the data is provided by the server API, in the interface
Use jquery Ajax to invoke the data, start how to call not, and do not report errors, the browser is now tested well, on the machine to load the local surface.
1, the server key code is as follows:
/**
* @see Httpservlet#doget (httpservletrequest request, HttpServletResponse
* Response)
*/
protected void doget (HttpServletRequest request,
httpservletresponse response) throws Servletexception, IOException {
Response.setcontenttype ("Text/javascript");//This should be noted
PrintWriter out = Response.getwriter ();
String callback = Request.getparameter ("callback");
Out.print (Callback
+ "([{name: ' John ', Age: '},{' name: ' Joe ', Age: '}]");
System.out.println ("callback =" + callback);
Out.flush ();
out.close ();
}
2, interface key code;
$.ajax ({
URL: "Http://localhost:8081/JsonpServer/JsonpServlet",
dataType: "Jsonp",
JSONP: "Callback",//pass to the request handler or page, to obtain the parameter name of the JSONP callback function name (default: Callback), and the server-side consistent
jsonpcallback: "Person",//Custom JSONP callback function name, default to jquery automatically generated random function name
success:function (JSON) {
//alert (json[0].name);
}
});
//equivalent to a function executed after overriding success (custom as Person)
function Person (JSON) {
alert ("I am person function ...");
alert (json[0].name);
}
Analysis: The first no effect is dataType: "JSON", so the test has not come out for a long time, and later modified into dataType: "Jsonp", you can use. The reason is that DataType is a JSON data format that cannot be accessed across domains andjsonp is accessed across domains.
Data types for Ajax