• 【Java Web开发学习】跨域请求


    【Java Web开发学习】跨域请求

    =================================================

    1、使用jsonp

    =================================================

    代码很简单不做解释
    json和jsonp的区别阅读   https://kb.cnblogs.com/page/139725/

    package ycx.crossdomain.controller;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class IndexController {
    
        @RequestMapping("/")
        public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("status", 200);
            dataMap.put("message", "OK");
            dataMap.put("data", "hello world");
    
            ObjectMapper mapper = new ObjectMapper();
            String data = mapper.writeValueAsString(dataMap);
    
            PrintWriter out = response.getWriter();
            String callback = request.getParameter("callback");
            if (StringUtils.isEmpty(callback)) {
                out.print(data);
            } else {
                out.print(callback + "(" + data + ")");
            }
            out.flush();
            out.close();
        }
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>jsonp</title>
        <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
    </head>
    <body>
    <script>
        // has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
        // $.ajax({
        //     type: "GET",
        //     async: false,
        //     url: "http://localhost:9902",
        //     success: function (result) {
        //         console.dir(result);
        //     },
        //     error: function (e) {
        //         console.dir(e);
        //     }
        // });
    
    
        $.ajax({
            type: "GET",
            async: false,
            url: "http://localhost:9902",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"handler",
            success: function (result) {
                console.dir(result);
            },
            error: function (e) {
                console.dir(e);
            }
        });
    </script>
    </body>
    </html>
  • 相关阅读:
    openssl之EVP系列之5---EVP_Encrypt系列函数具体解释(二)
    LINQ to SQL活学活用(1):这要打破旧观念
    【安卓】乾坤大罗移,将容器触摸事件传至还有一容器、!
    高速排序——JAVA实现(图文并茂)
    WebCollector爬取百度搜索引擎样例
    nyist 202 红黑树(二叉树中序遍历)
    excel转换日期格式,将yyyymmdd类型日期转换成yyyy-mm-dd等日期类型方法
    窗体界面设计03
    窗体界面设计02
    窗体界面设计01
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/11454362.html
Copyright © 2020-2023  润新知