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 启动服务

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

相关推荐
Jony_2 天前
高可用移动网络连接
网络协议
chilix2 天前
Linux 跨网段路由转发配置
网络协议
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅4 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19984 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_946205524 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel4 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)4 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库
~远在太平洋~4 天前
Debian系统如何删除多余的kernel
linux·网络·debian
unfeeling_4 天前
Keepalived实验
linux·服务器·网络