英文:
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="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.
答案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() {
$('#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);
}
});
});
});
<input type="button" value="Call Servlet" id="callServlet" />
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 :
$('#result').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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论