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);
                }
            );  


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM