今天工作需要调用一个c++程序,以给那个程序发送请求的方式,遇到了跨域问题

    function saatStart(){
        $.ajax({
            url:"http://192.168.1.202:8080/bracelet",
            data:{'id':$('#show_hand').val()},
            type:"post",
            success:function(result){
                json = eval(result);
                alert(json.DeviceCode);
                alert(json.InDBTime);
                //进入 enter、待命standby、离开exit
                alert(json.Status);
                _interval = setInterval("ajaxSaat()", 5000);
            }
        });

    }

    function saatStop(){
        $.ajax({
            url:"http://192.168.1.202:8080/bracelet",
            data:{'id':$('#show_hand').val()},
            type:"delete",
            success:function(result){
                json = eval(result);
                alert(json.DeviceCode);
                alert(json.InDBTime);
                //进入 enter、待命standby、离开exit
                alert(json.Status);
            }
        });
    }

犹豫c++那边是通过请求类型进行区别操作的,所以jsonp跨域直接放弃

然后百度了半天,发现可以通过java做一下代理的功能

ps:跨域问题:跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的内容),因为我们在日常的项目开发时会不可避免的需要进行跨域操作,所以跨域能力也算是前端工程师的基本功之一。

所以java是不存在跨域问题的:

js改为如下:

function saatStart(){
        var show_hand = $('#show_hand').val();
        $.ajax({
            url:"<%=path%>/regOne/saatStart.do",
            data:{'id':show_hand},
            type:"post",
            success:function(result){
                json = eval(result);
                alert(json.data);
                _interval = setInterval("ajaxSaat()", 5000);
            }
        });
    }

function saatStop(){
        var show_hand = $('#show_hand').val();
        $.ajax({
            url:"<%=path%>/regOne/saatStop.do",
            data:{id:show_hand},
            type:"post",
            success:function(result){
                json = eval(result);
                alert(json.data);
            }
        });
    }


java端代码:

@RequestMapping("/saatStart")
    public void saatStart(String id,HttpServletRequest req, HttpServletResponse resp) throws MalformedURLException, IOException {
        String url = "http://192.168.1.202:8080/bracelet";
        // 取得连接  
        HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();  
 
        // 设置连接属性  
        huc.setDoOutput(true);  
        huc.setRequestMethod("POST");  
        huc.setUseCaches(false);  
        huc.setInstanceFollowRedirects(true);  
        huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
        huc.connect();  
 
        // 往目标servlet中提供参数  
        OutputStream targetOS = huc.getOutputStream();  
        targetOS.write(("id="+id).getBytes());  
        targetOS.flush();  
        targetOS.close();  
 
        // 取得页面输出,并设置页面编码及缓存设置  
        resp.setContentType(huc.getContentType());  
        resp.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));  
        resp.setHeader("Pragma", huc.getHeaderField("Pragma"));  
        resp.setHeader("Expires", huc.getHeaderField("Expires"));  
        OutputStream os = resp.getOutputStream();  
 
        // 将目标servlet的输入流直接往页面输出  
        InputStream targetIS = huc.getInputStream();  
        int r;  
        while ((r = targetIS.read()) != -1) {  
            os.write(r);  
        }  
        targetIS.close();  
        os.flush();  
        os.close();  
 
        huc.disconnect();  
    }
    @RequestMapping("/saatStop")
    public void saatStop(String id,HttpServletRequest req, HttpServletResponse resp) throws MalformedURLException, IOException {
        String url = "http://192.168.1.202:8080/bracelet?id="+id;
        // 取得连接  
        HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();  
 
        // 设置连接属性  
        huc.setDoOutput(true);  
        huc.setRequestMethod("DELETE");  
        huc.setUseCaches(false);  
        huc.setInstanceFollowRedirects(true);  
        huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
        huc.connect();  
 
        // 取得页面输出,并设置页面编码及缓存设置  
        resp.setContentType(huc.getContentType());  
        resp.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));  
        resp.setHeader("Pragma", huc.getHeaderField("Pragma"));  
        resp.setHeader("Expires", huc.getHeaderField("Expires"));  
        OutputStream os = resp.getOutputStream();  
 
        // 将目标servlet的输入流直接往页面输出  
        InputStream targetIS = huc.getInputStream();  
        int r;  
        while ((r = targetIS.read()) != -1) {  
            os.write(r);  
        }  
        targetIS.close();  
        os.flush();  
        os.close();  
 
        huc.disconnect();  
    }

此处java发送delete请求无法写入参数,只能通过url?id=id这种方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值