本人java纯零基础,今天写了自己的第一个jsp代码,但是在请求ajax数据时,老是获取不到想要的数据。
需求是这样的,用户输入请求的页面,服务端解析请求的页面返回请求页面的html给客户端,我的代码是这样的
pageEncoding="ISO-8859-1"%>
Web SecurityString url = request.getParameter("url");
String html = "";
if (url != null && url != "") {
String htmpath = null;
BufferedReader in = null;
InputStreamReader isr = null;
InputStream is = null;
PrintWriter pw = null;
HttpURLConnection huc = null;
try {
htmpath = request.getParameter("username");
URL urlObj = new URL(url); //å建 URL
System.out.println(url);
huc = (HttpURLConnection) urlObj.openConnection();
is = huc.getInputStream();
isr = new InputStreamReader(is);
in = new BufferedReader(isr);
String line = null;
while (((line = in.readLine()) != null)) {
if (line.length() == 0)
continue;
//System.out.println(line);
html += line;
}
System.out.println(html);
System.out.println("hello");
}
catch (Exception e) {
//System.err.println(e);
out.println("xxx:" + e.getMessage());
} finally { //æ 论å¦ä½é½è¦å³éæµ
try {
is.close();
isr.close();
in.close();
huc.disconnect();
pw.close();
} catch (Exception e) {
}
}
}
%>
style="width: 100%; height: 100%;"> |
js
$(function(){
$("#btnStart").click(function(){
var theURL = $("#txtUrl").val();
alert("come in start URL:" + theURL);
$.ajax({
url: ' http://localhost:8080/index.jsp',
type: 'post',
data: 'url=' + theURL,
dataType: 'text',
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log('Error occurs at server.' + errorThrown);
alert('Error occurs at server.' + errorThrown);
},
success: function(data){
var s='';
alert(s)
alert("success:"+data);
console.log("" + data);
$("#ifm1").attr("srcdoc", data);
// if(data == "success"){
}
});
})
})
上面标红的地方的数据不是想要的数据,求高手指导。