Home > front end >  Some problems about ajax, for help ~ ~ ~
Some problems about ajax, for help ~ ~ ~

Time:10-08

Problem description: when in the provinces and cities linkage experiment, the appendChild added no child node, the previous line alert (pro. Pname); Also can display the normal province name, where are the great god please help see if the problem! Thank you very much

<% @ page language="Java" import="Java. Util. *" pageEncoding="utf-8" % & gt;
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % & gt;
<%
String path=request. GetContextPath ();
String=basePath request. GetScheme () + "://" + request. GetServerName () + ":" + request. GetServerPort () + + "/" path;
% & gt;




My JSP 'index. The JSP' starting page
<meta HTTP - equiv="pragma" content="no - cache" & gt;
<meta HTTP - equiv="cache-control" content="no - cache" & gt;
<meta HTTP - equiv="expires" content="0" & gt;
<meta HTTP - equiv="keywords" content="keyword1, keyword2, keyword3 & gt;"
<meta HTTP - equiv="description" content="This is my page" & gt;
<link rel="stylesheet" type="text/CSS" href="https://bbs.csdn.net/topics/styles.css" & gt;
- & gt;
The function createXMLHttpRequest () {
Try {
Return new XMLHttpRequest;//for most browser
} the catch (e) {
Try {
Return new ActiveXObject (" Msxml2. XMLHTTP ");//for IE6
} the catch (e) {
Try {
Return new ActiveXObject (" Microsoft. XMLHTTP ");//for IE5.5 and previous version
} the catch (e) {
Alert (" what you nong sha lai!" );
Throw e;
}
}
}
}
/*
After the success of the implementation content: 1, the page is loaded, visit ProvinceServlet get all provinces, added to the & lt; Select id="province" & gt;
2, give & lt; Select id="province" & gt; Add the onchange event listeners
When choosing send change, delete & lt; Select id="city" & gt; Besides the first line of data,
Visit CityServlet pid gain corresponding all by provinces city, is added to the & lt; Select id="city" & gt;
*/
Window. The onl oad=function () {
//AJAX four steps, request ProvinceServlet, get all the province name, use every province name to create a & lt; Option> Element,
//added to & lt; Select the name="province" & gt; The
Var xmlHttp=createXMLHttpRequest ();//get the xmlHttp object
Why can be used in the JS//C label, because the page on the server before sending to the client has been server converted into HTML
Var url="& lt; C: the url value='https://bbs.csdn.net/ProvinceServlet'/& gt; ";
XmlHttp. Open (" GET ", url, true);//transfer request, resource path, whether asynchronous
XmlHttp. Send (null);//send the request, the GET request body, but have to NULL, or FIREFIX might send failure, POST is used to pass parameters
XmlHttp. Onreadystatechange=function () {//response is complete set listeners
If (xmlHttp. ReadyState==4 & amp; & XmlHttp. Status==200) {
//the server status code 200 and xmlHttp status to 4:
Var text=xmlHttp. The responseText;//server for text formatting context
//the eval () get the JS object array
Var proArray=eval (" (" + text + ") ");
//to iterate over each province name, each name to generate an option object, added to the & lt; Select> The
for(var i=0; iVar pro=proArray [I];
Var optionEle=document. The createElement method (" option ");
OptionEle. Value=https://bbs.csdn.net/topics/pro.pid;
Var textNode=document. CreateTextNode (pro. Pname);
Alert (pro. Pname);
optionEle. The appendChild (textNode);
Documnet. GetElementById (" p "). The appendChild (optionEle);

}

}
};
};
</script>


<body>

Province:

City:



CodePudding user response:

Documnet. GetElementById (" p "). The appendChild (optionEle);
To:
Documnet. GetElementById (" p "). Append (optionEle);

CodePudding user response:

reference 1st floor XZZFXZ response:
documnet. GetElementById (" p "). The appendChild (optionEle);
To:
Documnet. GetElementById (" p "). Append (optionEle);



This no good ~ ~

CodePudding user response:

refer to the second floor sugar-coated bomb response:
Quote: refer to 1st floor XZZFXZ response:

Documnet. GetElementById (" p "). The appendChild (optionEle);
To:
Documnet. GetElementById (" p "). Append (optionEle);



This no good ~ ~

I tried it on, can be, but your code document spell wrong, put together the documnet

  • Related