跨域测试

1 GET

  • Usage

<html>
    <head>
        <meta charset="UTF-8">
        <title>title>
        <script src="https://cdn.bootcss.com/jquery/3.2.0/jquery.js">script>
    head>
    <body>
        <div id="">
            测试数据
        div>
        
        <script type="text/javascript">
            $.ajax({
                type:'get',
                url:'http://ip:port/resource-path',
                async:true,
                complete:function(data){
                    console.log(data);
                    res = JSON.parse(data.responseText)
                    console.log(res)
                } 
            }) 
        script>
        <div>div>
    body>
html>

2 POST

POST请求返回结果为Object,可直接使用.解析,如:
返回结果:

{
    "code": 200,
    "counts": 1,
    "infos": [
        {
            "name": "xiaohong",
            "address": "earth"
        }
    ]
}
  • 解析
data.code;
data.infos;
  • Usage

<html>
    <head>
        <meta charset="UTF-8">
        <title>title>
        <script src="https://cdn.bootcss.com/jquery/3.2.0/jquery.js">script>
    head>
    <body>
        <div id="">
            测试数据
        div>
        
        <script type="text/javascript">
            $.ajax({
                type:'post',
                url:'http://ip:port/uripath',
                contentType:"application/json;charset=utf-8",
                data:JSON.stringify({"name":"xiaohong","address":"earth"
                }),
                dataType:"json",
                success:function(data){
                    console.log("result!");
                    console.log(data);
                    console.log(data.code); 
                } 
            }) 
        script>
        <div>div>
    body>
html>

你可能感兴趣的:(#,单体服务SpringBoot2,#,Spring&MVC,#,测试ABC)