課程
/前端開(kāi)發(fā)
/JavaScript
/Ajax全接觸
ajax的URL與web.xml文件的URL是什么關(guān)系,還有這個(gè)例子是不是不用后臺(tái)代碼,總的來(lái)說(shuō)不是很懂a(chǎn)jax的URL
2016-10-13
源自:Ajax全接觸 5-1
正在回答
<script type="text/javascript">?? ?function ajax(){?? ??? ?? var xmlHttp=new XMLHttpRequest();//建立請(qǐng)求對(duì)象?? ??? ?? var phone=document.getElementById("phone") ;//獲得請(qǐng)求參數(shù)?? ??? ?? var order=document.getElementById("order");?? ??? ?? var url = "LoginServlet";//建立url連接?? ??? ?var data=phone.value;?? ??? ?? xmlHttp.open("post",url,true);//打開(kāi)服務(wù)器連接?? ??? ?? xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");? ?? ??? ?xmlHttp.send("phone="+data);//發(fā)送請(qǐng)求?? ??? ?xmlHttp.onreadystatechange = function() {//服務(wù)器響應(yīng)?? ??? ? if (xmlHttp.readyState == 4) {?? ??? ??? var response = xmlHttp.responseText;?? ??? ??? document.getElementById("order").innerHTML = response;?? ??? ????? }?? ??? ? }? }?? ?</script>
<script>
//要發(fā)送post請(qǐng)求,send發(fā)送請(qǐng)求那里我試了好像不行,監(jiān)聽(tīng)狀態(tài)的方法我是這么寫(xiě)的你看看吧
</script>
能幫我看下嗎,我的servlet獲取不到值,但前臺(tái)值已發(fā)送,謝謝啦
//servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String phone=request.getParameter("phone");//這塊獲取不到值,
//request.setAttribute("phone", phone);
System.out.println("adsdadad:"+phone);
}
//ajax
function ajax(){
? var xmlHttp=new XMLHttpRequest();//建立請(qǐng)求對(duì)象
? var phone=document.getElementById("phone") ;//獲得請(qǐng)求參數(shù)
? var order=document.getElementById("order");
? var url = "loginServlet";//建立url連接
var data=phone.value;
? xmlHttp.open("get",url,true);//打開(kāi)服務(wù)器連接
? xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
? xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(data);//發(fā)送請(qǐng)求
function updatePage() {//服務(wù)器響應(yīng)
?if (xmlHttp.readyState == 4) {
?
? ?var response = xmlHttp.responseText;
? ?
? ?document.getElementById("order").innerHTML = response;
?}
? }
?</script>
//web.xml
<servlet>
? ? <description>This is the description of my J2EE component</description>
? ? <display-name>This is the display name of my J2EE component</display-name>
? ? <servlet-name>loginSer</servlet-name>
? ? <servlet-class>sevlet.loginServlet</servlet-class>
? </servlet>
? <servlet-mapping>
? ? <url-pattern>/loginServlet</url-pattern>
? </servlet-mapping>
? <welcome-file-list>
? ? <welcome-file>ajax.html</welcome-file>
? </welcome-file-list>
ajax的 url就是方法的請(qǐng)求地址。請(qǐng)求后臺(tái)為什么不用后臺(tái)代碼,和后臺(tái)查詢的數(shù)據(jù)庫(kù)數(shù)據(jù)信息交互。應(yīng)該可以這么理解
慕UI8820655 提問(wèn)者
舉報(bào)
本課程通過(guò)一個(gè)案例,講解Ajax的相關(guān)概念原理實(shí)現(xiàn)方式和應(yīng)用
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-10-14
<script type="text/javascript">
?? ?function ajax(){
?? ??? ?? var xmlHttp=new XMLHttpRequest();//建立請(qǐng)求對(duì)象
?? ??? ?? var phone=document.getElementById("phone") ;//獲得請(qǐng)求參數(shù)
?? ??? ?? var order=document.getElementById("order");
?? ??? ?? var url = "LoginServlet";//建立url連接
?? ??? ?var data=phone.value;
?? ??? ?? xmlHttp.open("post",url,true);//打開(kāi)服務(wù)器連接
?? ??? ?? xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");?
?? ??? ?xmlHttp.send("phone="+data);//發(fā)送請(qǐng)求
?? ??? ?xmlHttp.onreadystatechange = function() {//服務(wù)器響應(yīng)
?? ??? ? if (xmlHttp.readyState == 4) {
?? ??? ??? var response = xmlHttp.responseText;
?? ??? ??? document.getElementById("order").innerHTML = response;
?? ??? ????? }
?? ??? ? }
? }
?? ?</script>
<script>
//要發(fā)送post請(qǐng)求,send發(fā)送請(qǐng)求那里我試了好像不行,監(jiān)聽(tīng)狀態(tài)的方法我是這么寫(xiě)的你看看吧
</script>
2016-10-14
能幫我看下嗎,我的servlet獲取不到值,但前臺(tái)值已發(fā)送,謝謝啦
//servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String phone=request.getParameter("phone");//這塊獲取不到值,
//request.setAttribute("phone", phone);
System.out.println("adsdadad:"+phone);
}
//ajax
function ajax(){
? var xmlHttp=new XMLHttpRequest();//建立請(qǐng)求對(duì)象
? var phone=document.getElementById("phone") ;//獲得請(qǐng)求參數(shù)
? var order=document.getElementById("order");
? var url = "loginServlet";//建立url連接
var data=phone.value;
? xmlHttp.open("get",url,true);//打開(kāi)服務(wù)器連接
? xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
? xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(data);//發(fā)送請(qǐng)求
function updatePage() {//服務(wù)器響應(yīng)
?if (xmlHttp.readyState == 4) {
?
? ?var response = xmlHttp.responseText;
? ?
? ?document.getElementById("order").innerHTML = response;
?}
}
? }
?</script>
//web.xml
<servlet>
? ? <description>This is the description of my J2EE component</description>
? ? <display-name>This is the display name of my J2EE component</display-name>
? ? <servlet-name>loginSer</servlet-name>
? ? <servlet-class>sevlet.loginServlet</servlet-class>
? </servlet>
? <servlet-mapping>
? ? <servlet-name>loginSer</servlet-name>
? ? <url-pattern>/loginServlet</url-pattern>
? </servlet-mapping>
? <welcome-file-list>
? ? <welcome-file>ajax.html</welcome-file>
? </welcome-file-list>
2016-10-14
ajax的 url就是方法的請(qǐng)求地址。請(qǐng)求后臺(tái)為什么不用后臺(tái)代碼,和后臺(tái)查詢的數(shù)據(jù)庫(kù)數(shù)據(jù)信息交互。應(yīng)該可以這么理解