读书人

AJAX获取服务器数据失败 告急该怎么解

发布时间: 2013-10-11 14:52:39 作者: rapoo

AJAX获取服务器数据失败 告急
近帮人做个网站,发现AJAX读取服务器数据老师失败,不知道是本地IIS问题还是。
http://center.smesd.gov.cn/util?o=areaselect&supercode=1030
上面是服务器地址,在浏览器中可以返回数据。

大家帮忙看看下面代码,或者有获取到数据并解析出来的支个招。。。


方式一:失败,改了好几种格式都不行。


function getcitylist1() {
$.ajax({
url: "http://center.smesd.gov.cn/util?o=areaselect&supercode=1030",
dataType: "text/html",
data: "",//
beforeSend: function(x) {
x.setRequestHeader("Content-Type", "text/html; charset=utf-8");//
},
success: function(json) {
alert(json.d);
},
error: function(x, e) {
alert(x.responseText);
},
complete: function(x) {
alert(x.responseText);
}
});

}


方式二:原始js完成的ajax,获取失败,用chrome查看了协议返回信息。

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错误:服务器内部错误");
}
}
}

function getcitylist2(){

doAjax("http://center.smesd.gov.cn/util?o=areaselect&supercode=1030","alert");

}




帮忙看看,调试一下。拜托各位了。。。。 ajax xmlhttprequest
[解决办法]
亲们,你们都不管跨域问题吗?

读书人网 >J2EE开发

热点推荐