AJAX 跨域调用和 Java 跨域 发送请求

前端之家收集整理的这篇文章主要介绍了AJAX 跨域调用和 Java 跨域 发送请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AJAX 跨域调用

前台代码

HTML代码

收藏代码

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

  2. <scripttype="text/javascript">

  3. functionRegisterRequest2(){

  4. /*getJSONmethodisokalso...

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

  6. */

  7. $.ajax({

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

  9. type:"GET",

  10. data:{pwd:'123',

  11. dataType:"jsonp",

  12. success:function(data){

  13. alert(data.name);

  14. },

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

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

  17. }

  18. });

  19. }

  20. </script>

  21. <inputtype="button"onclick="RegisterRequest2();"value="StartRequest"/>

后台Servlet代码

Java代码

收藏代码

  1. importjava.io.IOException;

  2. importjavax.servlet.ServletException;

  3. importjavax.servlet.http.HttpServlet;

  4. importjavax.servlet.http.HttpServletRequest;

  5. importjavax.servlet.http.HttpServletResponse;

  6. publicclassRigisterextendsHttpServlet{

  7. @Override

  8. protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)

  9. throwsServletException,IOException{

  10. Stringpwd=req.getParameter("pwd");

  11. Stringusername=req.getParameter("username");

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

  13. Stringcallback=req.getParameter("jsoncallback");

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

  15. }

  16. @Override

  17. protectedvoiddoPost(HttpServletRequestreq,IOException{

  18. Stringpwd=req.getParameter("pwd");

  19. Stringusername=req.getParameter("username");

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

  21. Stringcallback=req.getParameter("jsoncallback");

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

  23. }

  24. }

转载: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

猜你在找的Ajax相关文章