我在HTML中写了个获取城市列表的选框。在EditPlus编辑器中下面代码执行成功,并显示城市列表。当我直接点击打开在IE或CHROME中执行时却没有反应。。请问各位,这是咋回事。上面是执行成功的界面。下面是源码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
//2.采用js-ajax原生态语法-1:成功,下面所用
function createXmlHttp(){//创建AJAX(XmlHttpRequest)
var XmlHttp = false;
try{
XmlHttp = new XMLHttpRequest();
}catch(e1){
try{
XmlHttp = new ActiveXObject("Microsoft.XmlHttp");
}catch(e2){
try{
XmlHttp = new ActiveXObject("Msxml2.XmlHttp");
}catch(e3){
XmlHttp = false;
}
}
}
return XmlHttp;
}
/**
* 执行Ajax:url传参
*/
function doAjax(url,mothedName,state){
var astate=true;
if(state!=null){
if(state==false){
astate=false;
}
}
//alert(astate);
var XmlHttp =createXmlHttp();
if(!XmlHttp){//创建XmlHttpRequest
alert("此浏览器不支持AJAX");
}else{
//2 建立URL地址
var url = encodeURI(url);
//3 连接地址
XmlHttp.open("POST",url,astate);
//alert(url);
//4 从服务器上返回以后,调用ispinming函数
XmlHttp.onreadystatechange = function(){doAjaxNext(XmlHttp,mothedName);};
XmlHttp.setRequestHeader("If-Modified-Since","0");
//5 发送
XmlHttp.send(null);
}
}
function doAjaxNext(XmlHttp,mothedName){//调用的函数
if(XmlHttp.readyState==4){//正常
if(XmlHttp.status==200){// 正常
var result = unescape(XmlHttp.responseText);//从Action中返回的值
//alert(result);
if(result!=""){
if(mothedName){
eval(mothedName+"(result)");
}else{
doNext(result);//
}
}
}else if(XmlHttp.status==404){//错误
alert("404错误:找不到文件或目录");
}else if(XmlHttp.status==500){//错误
alert("500错误:服务器内部错误");
}
}
}</script>
<body><form id="form1" name="form1">
<p><select id="city_id_0" name="a_sheng" onchange="getshengshixian1(0)"><option value="">请选择省份</option></select>
<select id="city_id_1" name="a_shi" onchange="getshengshixian1(1)"><option value="">请选择地级市</option></select>
<select id="city_id_2" name="a_xian"><option value="">请选择市/县级市/县</option></select>
<br>
<script type="text/javascript">
//列出省市县 省:0 市:1 县:2
function getshengshixian1(jibie){
if(jibie == 0){
var sheng = document.form1.a_sheng.value;
//alert(sheng);
list_shi1(sheng);
}else if(jibie == 1){
var shi = document.form1.a_shi.value;
//alert(shi);
list_xian1(shi);
}
}
var city_form_id1; function addSelectList1(obj){
var list = document.getElementById(city_form_id1);
var bb=obj.split(";");
var cc;
clearList1(list);
for(var i = 0; i <bb.length; i++){
var newOption = document.createElement("option");
cc = bb[i].split(",");
newOption.setAttribute("value", cc[0]);
newOption.appendChild(document.createTextNode(cc[1]));
list.appendChild(newOption);
}
//alert(list);
}
function clearList1(list){
for(var i = list.options.length; i >= 1; i--){
list.remove(i);
}
}
function list_sheng1(){
city_form_id1="city_id_0";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode=1030","doNext1");
var list = document.getElementById("city_id_1");
if(list){
clearList1(list);
}
}
function list_shi1(supercode){
city_form_id1="city_id_1";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode="+supercode,"doNext1");
var list = document.getElementById("city_id_2");
if(list){
clearList1(list);
}
}
function list_xian1(supercode){
city_form_id1="city_id_2";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode="+supercode,"doNext1");
} function doNext1(obj){//obj:data
addSelectList1(obj);
} list_sheng1();
//getshengshixian1(0); </script>
</form></body>
</html>ajaxjs
<head>
<title>无标题页</title>
</head>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
//2.采用js-ajax原生态语法-1:成功,下面所用
function createXmlHttp(){//创建AJAX(XmlHttpRequest)
var XmlHttp = false;
try{
XmlHttp = new XMLHttpRequest();
}catch(e1){
try{
XmlHttp = new ActiveXObject("Microsoft.XmlHttp");
}catch(e2){
try{
XmlHttp = new ActiveXObject("Msxml2.XmlHttp");
}catch(e3){
XmlHttp = false;
}
}
}
return XmlHttp;
}
/**
* 执行Ajax:url传参
*/
function doAjax(url,mothedName,state){
var astate=true;
if(state!=null){
if(state==false){
astate=false;
}
}
//alert(astate);
var XmlHttp =createXmlHttp();
if(!XmlHttp){//创建XmlHttpRequest
alert("此浏览器不支持AJAX");
}else{
//2 建立URL地址
var url = encodeURI(url);
//3 连接地址
XmlHttp.open("POST",url,astate);
//alert(url);
//4 从服务器上返回以后,调用ispinming函数
XmlHttp.onreadystatechange = function(){doAjaxNext(XmlHttp,mothedName);};
XmlHttp.setRequestHeader("If-Modified-Since","0");
//5 发送
XmlHttp.send(null);
}
}
function doAjaxNext(XmlHttp,mothedName){//调用的函数
if(XmlHttp.readyState==4){//正常
if(XmlHttp.status==200){// 正常
var result = unescape(XmlHttp.responseText);//从Action中返回的值
//alert(result);
if(result!=""){
if(mothedName){
eval(mothedName+"(result)");
}else{
doNext(result);//
}
}
}else if(XmlHttp.status==404){//错误
alert("404错误:找不到文件或目录");
}else if(XmlHttp.status==500){//错误
alert("500错误:服务器内部错误");
}
}
}</script>
<body><form id="form1" name="form1">
<p><select id="city_id_0" name="a_sheng" onchange="getshengshixian1(0)"><option value="">请选择省份</option></select>
<select id="city_id_1" name="a_shi" onchange="getshengshixian1(1)"><option value="">请选择地级市</option></select>
<select id="city_id_2" name="a_xian"><option value="">请选择市/县级市/县</option></select>
<br>
<script type="text/javascript">
//列出省市县 省:0 市:1 县:2
function getshengshixian1(jibie){
if(jibie == 0){
var sheng = document.form1.a_sheng.value;
//alert(sheng);
list_shi1(sheng);
}else if(jibie == 1){
var shi = document.form1.a_shi.value;
//alert(shi);
list_xian1(shi);
}
}
var city_form_id1; function addSelectList1(obj){
var list = document.getElementById(city_form_id1);
var bb=obj.split(";");
var cc;
clearList1(list);
for(var i = 0; i <bb.length; i++){
var newOption = document.createElement("option");
cc = bb[i].split(",");
newOption.setAttribute("value", cc[0]);
newOption.appendChild(document.createTextNode(cc[1]));
list.appendChild(newOption);
}
//alert(list);
}
function clearList1(list){
for(var i = list.options.length; i >= 1; i--){
list.remove(i);
}
}
function list_sheng1(){
city_form_id1="city_id_0";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode=1030","doNext1");
var list = document.getElementById("city_id_1");
if(list){
clearList1(list);
}
}
function list_shi1(supercode){
city_form_id1="city_id_1";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode="+supercode,"doNext1");
var list = document.getElementById("city_id_2");
if(list){
clearList1(list);
}
}
function list_xian1(supercode){
city_form_id1="city_id_2";//
doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode="+supercode,"doNext1");
} function doNext1(obj){//obj:data
addSelectList1(obj);
} list_sheng1();
//getshengshixian1(0); </script>
</form></body>
</html>ajaxjs
XMLHTTP只有IE支持,这个是标准解答
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
如果js不是很牛的话,建议直接使用jquery吧。
jquery至少能解决60%的兼容性问题。
我用jQuery试过了 试了get post ajax 连数据都获取不到 这有个链接 其中是返回一堆字符串 内容你可以用浏览器直接访问查看 。 能帮我写个jQuery.ajax访问该链接的代码吗?我不知道怎么设置这些头信息类型。
http://center.smesd.gov.cn/util?o=areaselect&supercode=1030
谢谢啊 jQuery用了不过在EDITPLUS中都没反应 这有个链接 用上面代码在EditPlus中执行成功 但浏览器失败 如果jQuery你很熟悉的话 能帮我写个获取这个链接的代码吗?
http://center.smesd.gov.cn/util?o=areaselect&supercode=1030
var params =""; $.ajax(
{
type:'post',
url:url,
data:params,
dataType:'text',
success:function(msg)
{
alert("success"+msg);
var obj = JSON.parse(msg);//json字符串转换为json对象
},
error:function()
{
alert("error"+msg);
}
});