AJAX 跨域调用
<scripttype="text/javascript"src="jquery-1.7.2.min.js"></script>
<scripttype="text/javascript">
functionRegisterRequest2(){
/*getJSONmethodisokalso...
$.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',
dataType:"jsonp",
success:function(data){
alert(data.name);
},
error:function(a,b,c){
alert("error=="+b);
}
});
}
</script>
<inputtype="button"onclick="RegisterRequest2();"value="StartRequest"/>
Java代码
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassRigisterextendsHttpServlet{
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
Stringpwd=req.getParameter("pwd");
Stringusername=req.getParameter("username");
System.out.println("get=="+username+"==="+pwd);
Stringcallback=req.getParameter("jsoncallback");
resp.getWriter().write(callback+"({name:'tzy',phone:'123'})");//参数为json格式
}
@Override
protectedvoiddoPost(HttpServletRequestreq,IOException{
Stringpwd=req.getParameter("pwd");
Stringusername=req.getParameter("username");
System.out.println("post=="+username+"==="+pwd);
Stringcallback=req.getParameter("jsoncallback");
resp.getWriter().write(callback+"({name:'tzy',phone:'123'})");
}
}
转载:http://tonl.iteye.com/blog/1682394
Java 跨域 发送请求
packagecom.nicchagil.httprequestdemo;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLConnection;publicclassHttpPostRequest{/** *Main *@paramargs *@throwsException */ publicstaticvoidmain(String[]args)throwsException{ System.out.println(doPost()); } /** *PostRequest *@return *@throwsException*/ publicstaticStringdoPost()throwsException{ StringparameterData="username=nickhuang&blog=http://www.cnblogs.com/nick-huang/"; URLlocalURL=newURL("http://localhost:8080/OneHttpServer/"); URLConnectionconnection=localURL.openConnection(); HttpURLConnectionhttpURLConnection=(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())); OutputStreamoutputStream=null; OutputStreamWriteroutputStreamWriter=null; InputStreaminputStream=null; InputStreamReaderinputStreamReader=null; BufferedReaderreader=null; StringBufferresultBuffer=newStringBuffer(); StringtempLine=null; try{ outputStream=httpURLConnection.getOutputStream(); outputStreamWriter=newOutputStreamWriter(outputStream); outputStreamWriter.write(parameterData.toString()); outputStreamWriter.flush(); if(httpURLConnection.getResponseCode()>=300){thrownewException("HTTPRequestisnotsuccess,Responsecodeis"+httpURLConnection.getResponseCode()); } inputStream=httpURLConnection.getInputStream(); inputStreamReader=newInputStreamReader(inputStream); reader=newBufferedReader(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(); } }returnresultBuffer.toString(); } }
转载:http://www.cnblogs.com/nick-huang/p/3859353.html
原文链接:https://www.f2er.com/ajax/161993.html