2 luochunlin855520 luochunlin855520 于 2017.09.07 18:27 提问

jsp 页面怎么通过按钮提交POST请求,并获取返回值。 5C

我用过ajax,jquery,提交请求可以,但是返回值获取不到,提示跨域请求的问题。直接放弃,因为我是接口请求。

index.jsp
<%@ page language="java" import="java.util.*" %>
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import="WEB_API.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">









<%
WEB_API wa=new WEB_API();
String res=wa.sendPost("17761280053", "验证码是123");
System.out.println(res);
%>

<br> function dianji(){<br> $.post(&quot;<a href="http://eucpwx.mb345.com:443/AppApi/User/GetUserList?UserID=SU0126&amp;GetType=1&amp;NowID=&amp;GetNum=1&amp;Other=&amp;SearchUserName=">http://eucpwx.mb345.com:443/AppApi/User/GetUserList?UserID=SU0126&amp;GetType=1&amp;NowID=&amp;GetNum=1&amp;Other=&amp;SearchUserName=</a>&quot;, function(data){<br> alert(&quot;Data Loaded: &quot; + data);<br> });</p> <pre><code> } &lt;/script&gt; &lt;/head&gt; </code></pre> <p><body>中文显示示例 <br><br> <input id="test" type="text" value="jquery"/><br> <input id="btn" type="button" onclick="dianji()" value="单击弹窗"/> <br> </body><br> </html></p> <p>WEB_API.java<br> package WEB_API;</p> <p>import java.awt.EventQueue;<br> import java.io.BufferedReader;<br> import java.io.IOException;<br> import java.io.InputStreamReader;<br> import java.io.PrintWriter;<br> import java.io.UnsupportedEncodingException;<br> import java.net.MalformedURLException;<br> import java.net.URL;<br> import java.net.URLConnection;<br> import java.net.URLEncoder;<br> import javax.swing.*;<br> import java.sql.Connection;<br><br> import java.sql.DatabaseMetaData;<br><br> import java.sql.DriverManager;<br><br> import java.sql.PreparedStatement;<br> import java.sql.ResultSet;<br><br> import java.sql.ResultSetMetaData;<br><br> import java.sql.SQLException;<br><br> import java.sql.Statement;<br><br> import java.sql.Types;<br> import java.io.BufferedReader;<br><br> import java.io.File;<br><br> import java.io.FileOutputStream;<br><br> import java.io.FileReader;<br><br> import java.io.RandomAccessFile;<br> import java.util.ArrayList;<br> import java.util.List;<br> import com.alibaba.fastjson.JSON.*;</p> <p>public class WEB_API{<br><br> private static Connection con = null; </p> <pre><code>public static void main(String[] args) { } public WEB_API(){ } public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = &quot;&quot;; String strUrl=&quot;http://eucpwx.mb345.com:443/AppApi/User/GetUserList?&quot;; String strparam = &quot;UserID=SU0126&amp;GetType=1&amp;NowID=&amp;GetNum=1&amp;Other=&amp;SearchUserName=&quot;; try { URL realUrl = new URL(strUrl); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty(&quot;accept&quot;, &quot;*/*&quot;); conn.setRequestProperty(&quot;connection&quot;, &quot;Keep-Alive&quot;); conn.setRequestProperty(&quot;user-agent&quot;, &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)&quot;); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(strparam); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(),&quot;UTF-8&quot;)); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(&quot;发送POST请求出现异常!&quot; + e); e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } </code></pre> <p>} </p> <p><img src="http://img.ask.csdn.net/upload/201709/07/1504780002_204864.png" alt="图片说明"></p>

3个回答

zhwyj1019
zhwyj1019   2017.09.07 23:08
 <script>
        $(function () {
            $("#btn").click(function () {

                $("form1").submit();
            });
        });
    </script>


    <form method="post" action="#" id="form1">

            <input id="btn" type="button" value="点我"/>
        </form>

应该是post提交,但是不知道是不是你想要的。

xiaoming120915
xiaoming120915   2017.09.08 09:07

ajax跨域请求网上有很多解决方法的,你可以去搜搜jsonp

li1340089590
li1340089590   2017.09.08 09:29

jsonp

看下这能不能解决你的问题 http://justcoding.iteye.com/blog/1366102

Csdn user default icon
上传中...
上传图片
插入图片