Data types for Ajax

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.