<SCRIPT language=javascript>
     var xmlhttp;

//初始化xmlhttp
if(window.XMLHttpRequest)//IE7, Mozilla ,Firefox等浏览器内置该对象
{
      xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)//IE6,IE=5
{
      try 
      { 
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } 
catch (e) 
{ }

      if (xmlhttp == null) 
      {
      try 
      { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } 
catch (e) 
{ }
      }
}
//取得用户的其他资源
xmlhttp.open("POST",'LastestResource.aspx',false);
xmlhttp.send();

show1=xmlhttp.responseText;//alert(show1);
document.getElementById('latestrs').innerHTML=show1;</SCRIPT>这段代码在页面中,如果该页面不加form就可以正常执行,显示数据,为什么加上form就不行了呢?

解决方案 »

  1.   

    post的时候注意setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded");推荐你个ajax方法:
    /*
    ** <summary>
    ** ajax应用
    ** </summary>
    ** <param name="url">资源地址</param>
    ** <param name="callback">回调方法</param>
    ** <param name="method">请求方式</param>
    ** <param name="data">所发送数据</param>
    */
    sl.ajax = function(url ,callback ,method ,data){
    var method = method ? method : "get";
    var data = data ? data : null;
    var url = (method == "get") ? (url +"?"+ data) : url;
    var http = window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

    var doGet = function(){
    http.open(method ,url ,false);
    http.setRequestHeader("If-Modified-Since" , "0" );
    http.send(null);
    callback(http.responseText);
    };

    var doPost = function(){
    http.open(method ,url ,false);
    http.setRequestHeader("content-length" ,data ?data.length : 0);
    http.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded");
    http.send(data);
    callback(http.responseText);
    };

    if (method == "post")
    {
    doPost();
    }
    else
    {
    doGet();
    }
    http = null;
    };