3 ways to solve the problem of Ajax cross-domain
1, with the response header: Add the response header on the server:
The Page Setup response header allows Ajax cross-domain access: both:
2, using the proxy: In the current server load a proxy server, Proxy Server allows Ajax cross-domain, in a proxy server sent to the primary server;
3. Using Jsonp:
<script src= "Http://localhost:3000/showAll" ></script>src point to the address of the server you want to link to, the data returned by the service as JS code execution; This is a jsonp principle, The equivalent of using a script tag to send an AJAX-like request (only the request address); Request http://localhost:3000 the ShowAll method below;
Script,link img and other tags can be cross-domain;
Methods of jquery Encapsulation
Using the Jsonp method, add the following two parameters to Ajax;
1, datatype and jsonpcallback two parameters in $.ajax, the script method is encapsulated into Ajax form, then as long as the datatype is not Ajax, is Jsonp method;
2, principle: First in use $. Ajax generates a script tag, and then defines a method to receive the returned data to implement cross-domain, but here this Ajax is not Ajax, the implementation of the mechanism is actually SJONP, sensitive data do not cross-domain;
3, JOSNP does not support post method;
4, $.ajax ({type: "Get", url: "Http://localhost:3000/showAll", DataType: "Jsonp", Jsonpcallback: "CD" (defines the method name that the server returns, If the server returned is not a method, can be omitted;),})
Cross-domain issues