zoukankan      html  css  js  c++  java
  • java 后台实现ajax post跨域请求传递json格式数据获取json数据问题

    参考大神:http://blog.csdn.net/chunqiuwei/article/details/19924821

    java后台:
     
    public String ajaxProxy(Integer param1,String param2,String url, HttpServletRequest req){   
            JSONObject node = new JSONObject();         
     try {    
        node.put("param1", param1) ;
         node.put("param2", param2);                               
        } catch (JSONException e1) {  
            e1.printStackTrace();  
        }        
          
        // 使用POST方式向目的服务器发送请求  
        URL connect;  
        StringBuffer data = new StringBuffer();  
        try {  
            connect = new URL(url);  
            HttpURLConnection connection = (HttpURLConnection)connect.openConnection();  
            connection.setRequestMethod("POST");  
            connection.setDoOutput(true); 
            connection.setRequestProperty("Content-Type", "application/json");
             
            OutputStreamWriter paramout = new OutputStreamWriter(  
                    connection.getOutputStream(),"UTF-8");  
            paramout.write(node.toString());  
            paramout.flush();  
      
            BufferedReader reader = new BufferedReader(new InputStreamReader(  
                    connection.getInputStream(), "UTF-8"));  
            String line;              
            while ((line = reader.readLine()) != null) {          
                data.append(line);            
            }  
            paramout.close();  
            reader.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return data.toString();  
        }


    前台jsp: 

    $.post(
                    url,
                    {
                        param1:param1,
                        param2:param2,
                        url:url
                    },
                    function (data){
                        alert(data);
                    }
                );  


     

  • 相关阅读:
    第一章 zookeeper基础概念
    ntp服务
    nfs与dhcp服务
    samba、ftp和ssh服务
    Linux系统的RAID磁盘阵列
    Linux系统中lvm简介
    Linux系统中的计划任务
    Linux权限管理
    Linux用户及组管理
    简谈OSI七层模型(网络层)
  • 原文地址:https://www.cnblogs.com/super-chao/p/8940590.html
Copyright © 2011-2022 走看看