从jsp文件发送GET请求并获取JSON响应

huangapple go评论43阅读模式
英文:

Sending Get Request from a jsp file and getting json response

问题

以下是您要翻译的内容:

"I would like to perform a get request from a JSP file and get display the json response.

The link for the get request will look like this:

https://api.fda.gov/drug/label.json?search="testicular cancer"

However I would like to replace the search parameter with an input parameter from an html form

The html form is in the index.html file:

<html>
<body>
<form action="login.jsp" method="post">
<input type="text" name="disease" placeholder="Enter Disease Name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

In the login.jsp file I wrote the following code, based on a suggestion to use ajax (from the comments):

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
function calling(){
$.ajax({
url : 'https://api.fda.gov/drug/label.json?search="testicular cancer"',
type : 'GET',
dataType : 'json',//
contentType : 'application/json',
complete : function(response) {
console.log(response);
}
});
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<input type="button" value="Call Servlet" name="Call Servlet" onclick="calling()"/>
</body>
</html>

However I can't see any results when I click the button "Call Servlet". What can I do to correct it?

I am using tomcat in ubuntu 18.04."

请注意,我已经去掉了代码部分,只返回了翻译好的文本。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

I would like to perform a get request from a JSP file and get display the json response.

The link for the get request will look like this :

https://api.fda.gov/drug/label.json?search=&quot;testicular cancer&quot;

However I would like to replace the search parameter with an input parameter from an html form

The html form is in the index.html file:

 &lt;html&gt;
  &lt;body&gt;
    &lt;form action=&quot;login.jsp&quot; method=&quot;post&quot;&gt;
      &lt;input type=&quot;text&quot; name=&quot;disease&quot; placeholder=&quot;Enter Disease Name&quot;/&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

In the login.jsp file I wrote the following code, based on a suggestion to use ajax (from the comments):

&lt;html&gt;
&lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
       function calling(){
       	 $.ajax({
            url : &#39;https://api.fda.gov/drug/label.json?search=&quot;testicular cancer&quot;&#39;,
            type : &#39;GET&#39;,
            dataType : &#39;json&#39;,// 
            contentType : &#39;application/json&#39;,
            complete : function(response) {
                 console.log(response);  
                }
        });
       }
    &lt;/script&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
    &lt;title&gt;JSP Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;input type=&quot;button&quot; value=&quot;Call Servlet&quot; name=&quot;Call Servlet&quot;  onclick=&quot;calling()&quot;/&gt;
&lt;/body&gt;
&lt;/html&gt;

However I can't see any results when I click the button "Call Servlet". What can I do to correct it?

I am using tomcat in ubuntu 18.04.

答案1

得分: 0

你可以将HTML中的onClick移除,然后在jQuery的ready事件中添加它,如下所示:

$(document).ready(function() {
   $('#callServlet').click(function(){
     $.ajax({
        url : 'https://api.fda.gov/drug/label.json?search="testicular cancer"',
        type : 'GET',
        dataType : 'json',
        contentType : 'application/json',
        success: function(response) {
             console.log(response);  
            }
        error: function(response) {
             console.log(response);  
            }
    });
   });
});

或者你可以继续在HTML中使用onClick触发器,就像你现在已经做的那样,只需确保为ajax设置成功和错误回调函数。

如果你想要将JSON显示在一个带有id的div中,比如result,那么可以像下面这样做:

$('#result').html(JSON.stringify(response));

将上面的代码添加到ajax的成功或错误回调函数中,以将响应作为JSON字符串添加到HTML中。

英文:

You could remove the onClick from html and add it to jquery on ready like :

$(document).ready(function() {
       $(&#39;#callServlet&#39;).click(function(){
         $.ajax({
            url : &#39;https://api.fda.gov/drug/label.json?search=&quot;testicular cancer&quot;&#39;,
            type : &#39;GET&#39;,
            dataType : &#39;json&#39;,// 
            contentType : &#39;application/json&#39;,
            success: function(response) {
                 console.log(response);  
                }
            error: function(response) {
                 console.log(response);  
                }
        });
       });
});

&lt;input type=&quot;button&quot; value=&quot;Call Servlet&quot; id=&quot;callServlet&quot; /&gt;

or just use the onClick trigger in html like you have donw now, just make sure to have success as well as error callbacks for ajax.

If you want the json to be displayed in a div with an id like result then do like for example :

$(&#39;#result&#39;).html(JSON.stringify(response));

Add the above code to the success or error callbacks of an ajax to add the response to the html as json string.

huangapple
  • 本文由 发表于 2020年7月29日 22:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63155658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定