RPC(5):AJAX跨域请求处理

接上一篇RPC(4):HttpClient实现RPC之POST请求进行修改。

1 修改客户端项目

1.1 修改maven文件

修改后配置文件如下:

复制代码
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.2.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>httpclient_rpc_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.1</version>
        </dependency>
    </dependencies>

1.2 新增页面

在resource下的static中新增index.html页面(提前引入jquery)

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
        var url = "123";
        // javascript中默认的,ajax请求不能跨域。
        // 跨域 - ajax所属的站点,和被请求的站点,不是同一个域。
        // 域 - ip,端口,域名,主机名,任何一个有变化,都是不同域。
        // 需要服务器,返回的响应中增加跨域请求头。
        function sendBodyParams(){
            $.ajax({
                "url": "http://localhost:80/bodyParams",
                "type":"post",
                "data":"[{\"name\":\"abc\", \"password\":\"123\"},{\"name\":\"def\", \"password\":\"321\"}]",
                "contentType":"application/json", // 必须设定,代表请求体的格式。默认是text/plain; 默认是 参数名=参数值&参数名=参数值 的字符串
                "dataType":"json",
                "success":function(data){
                    alert(data);
                    console.log(data);
                }
            });
        }
    </script>
</head>
<body style="text-align: center">
    <button onclick="sendBodyParams()">测试AJAX请求,请求体传递JSON参数</button>
</body>
</html>

1.3 编写启动类

复制代码
package com.example.httpclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApp {
    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

2 修改服务端项目

在bodyParams中新增@CrossOrigin方法。

3 启动服务

访问页面,点击按钮,结果如下:

相关推荐
brave and determined11 分钟前
接口通讯学习(day04):RS-232与RS-485:通信接口全解析
网络·uart·通讯·emc·rs232·rs485·嵌入式设计
檀越剑指大厂11 分钟前
在家也能远程调代码?WSL+cpolar 的实用技巧分享
网络
秋邱17 分钟前
价值升维!公益赋能 + 绿色技术 + 终身学习,构建可持续教育 AI 生态
网络·数据库·人工智能·redis·python·学习·docker
爱学习的大牛12338 分钟前
如何系统学习网络渗透测试:从入门到精通的完整指南
网络·学习
程序猿编码40 分钟前
PRINCE算法的密码生成器:原理与设计思路(C/C++代码实现)
c语言·网络·c++·算法·安全·prince
飞行增长手记1 小时前
什么是高匿代理IP?安全吗?怎么选?
网络协议·tcp/ip·安全
white-persist2 小时前
【攻防世界】reverse | Reversing-x64Elf-100 详细题解 WP
c语言·开发语言·网络·python·学习·安全·php
HKT_China2 小时前
香港电讯与Fortinet推出100G高效加密网络托管服务,迈进量子安全新时代
网络·安全
上海云盾安全满满6 小时前
高防 IP 是如何帮助数藏行业防刷
网络·网络协议·tcp/ip
吠品8 小时前
免费SSL证书自动化申请:DNS代理验证
网络协议·自动化·ssl