AJAX 跨域调用

前台代码:

Html代码  AJAX 跨域调用和 Java 跨域 发送请求_Java 跨域

  1. <script type="text/javascript" src="jquery-1.7.2.min.js"></script>   

  2. <script type="text/javascript">  

  3.     function RegisterRequest2() {  

  4.         /* getJSON method is ok also...   

  5.         $.getJSON("http://192.168.1.154:7087/ClusterServer/Rigister?jsoncallback=?", { pwd: '123', username: 'tl' }, function(json) { alert(json.name); });   

  6.         */  

  7.            

  8.         $.ajax({  

  9.             url: "http://192.168.1.154:7087/ClusterServer/Rigister?jsoncallback=?",  

  10.             type: "GET",  

  11.             data: { pwd: '123', username: 'tl' },  

  12.             dataType: "jsonp",  

  13.             success: function(data) {  

  14.                 alert(data.name);  

  15.             },  

  16.             error: function(a, b, c) {  

  17.                 alert("error==" + b);  

  18.             }  

  19.   

  20.         });   

  21.     }    

  22. </script>  

  23. <input type="button" onclick="RegisterRequest2();"  value="StartRequest" />  

 

后台Servlet代码:

Java代码  AJAX 跨域调用和 Java 跨域 发送请求_Java 跨域

  1. import java.io.IOException;  

  2.   

  3. import javax.servlet.ServletException;  

  4. import javax.servlet.http.HttpServlet;  

  5. import javax.servlet.http.HttpServletRequest;  

  6. import javax.servlet.http.HttpServletResponse;  

  7.   

  8. public class Rigister extends HttpServlet {  

  9.   

  10.     @Override  

  11.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  

  12.             throws ServletException, IOException {   

  13.         String pwd = req.getParameter("pwd");  

  14.         String username =req.getParameter("username");  

  15.         System.out.println("get=="+username+"  ==="+pwd);  

  16.            

  17.         String callback = req.getParameter("jsoncallback");   

  18.         resp.getWriter().write(callback + "({name:'tzy',phone:'123'})"); //参数为json格式  

  19.     }  

  20.   

  21.     @Override  

  22.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  

  23.             throws ServletException, IOException {   

  24.         String pwd = req.getParameter("pwd");  

  25.         String username =req.getParameter("username");  

  26.         System.out.println("post=="+username+"  ==="+pwd);  

  27.            

  28.         String callback = req.getParameter("jsoncallback");   

  29.         resp.getWriter().write(callback + "({name:'tzy',phone:'123'})");   

  30.     }  

  31.   


转载:http://tonl.iteye.com/blog/1682394

Java 跨域 发送请求


package com.nicchagil.httprequestdemo;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;public class HttpPostRequest {    /**
     * Main
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        System.out.println(doPost());
    }    
    /**
     * Post Request
     * @return
     * @throws Exception     */
    public static String doPost() throws Exception {
        String parameterData = "username=nickhuang&blog=http://www.cnblogs.com/nick-huang/";
        
        URL localURL = new URL("http://localhost:8080/OneHttpServer/");
        URLConnection connection = localURL.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));
        
        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;        
        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);
            
            outputStreamWriter.write(parameterData.toString());
            outputStreamWriter.flush();            
            if (httpURLConnection.getResponseCode() >= 300) {                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
        } finally {            
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }            
            if (outputStream != null) {
                outputStream.close();
            }            
            if (reader != null) {
                reader.close();
            }            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }        return resultBuffer.toString();
    }

}

转载:http://www.cnblogs.com/nick-huang/p/3859353.html