AJAX 跨域调用
前台代码:
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function RegisterRequest2() {
/* getJSON method is ok also...
$.getJSON("http://192.168.1.154:7087/ClusterServer/Rigister?jsoncallback=?", { pwd: '123', username: 'tl' }, function(json) { alert(json.name); });
*/
$.ajax({
url: "http://192.168.1.154:7087/ClusterServer/Rigister?jsoncallback=?",
type: "GET",
data: { pwd: '123', username: 'tl' },
dataType: "jsonp",
success: function(data) {
alert(data.name);
},
error: function(a, b, c) {
alert("error==" + b);
}
});
}
</script>
<input type="button" onclick="RegisterRequest2();" value="StartRequest" />
后台Servlet代码:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Rigister extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String pwd = req.getParameter("pwd");
String username =req.getParameter("username");
System.out.println("get=="+username+" ==="+pwd);
String callback = req.getParameter("jsoncallback");
resp.getWriter().write(callback + "({name:'tzy',phone:'123'})"); //参数为json格式
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String pwd = req.getParameter("pwd");
String username =req.getParameter("username");
System.out.println("post=="+username+" ==="+pwd);
String callback = req.getParameter("jsoncallback");
resp.getWriter().write(callback + "({name:'tzy',phone:'123'})");
}
}
转载: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