jquery uses JSONP to access __ garbled problems across domains

Source: Internet
Author: User

Powerful jquery has been supporting JSONP since 1.2, and today we use the servlet to do a little test in the background.

front-end JavaScript (using Jsonpcallback as a callback function, where the success does not work):

function Loadjsonp () {
    	$.ajax ({
             URL: ' http://192.168.28.128:8080/JsonPDemo/servlet/JsonPServlet?callback= Jsonpcallback ',
             dataType: "Jsonp",
             success:function (data) {
                 alert (data);
             }
        );
    function Jsonpcallback (data) {
    	alert (data[0][' name ');
    }
Background (get the callback function of the foreground jsonp, Output jsonpcallback (...) ):
public void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		Response.setcontenttype ("text/html");
		Response.setcharacterencoding ("Utf-8");
		PrintWriter out = Response.getwriter ();
		String Jsonp = Request.getparameter ("callback");
		String str = "[{' id ': ' 1 ', ' name ': ' Test 1 '},{' id ': ' 2 ', ' name ': ' Test 2 '}]";
		str = JSONP + "(" + str + ")";
		Out.println (str);
		Out.flush ();
		Out.close ();
	}

Careful friends find that JSONP is not a good technique, he just put the JSON string in the callback function as a parameter in the callback, and then let the front-end call. However, Jsonp is indeed a clever way to address cross-domain access.


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.