开发者

How to create a xmlhttp request?

开发者 https://www.devze.com 2022-12-11 06:43 出处:网络
var url=\"display.php?vote=\"+grade; xmlHttp.onreadystatechange=stateChanged xmlHttp.open(\"GET\",url,true)
var url="display.php?vote="+grade; 
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

This piece of code fails to send out the request.开发者_StackOverflow社区 How to create a xmlHttp correctly?


<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
}
</script>

this piece of code is available in link text you can learn basics here like i did. hope this helps.


Here is a "80%" solution.

function GetXHR()
{
  try
  {
    if (window.XmlHTTPRequest)
      xmlHttp = new XmlHTTPRequest()
    else
      xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0")
  }
  catch(e) { }
}

var xmlHttp = GetXHR()
if (xmlHttp)
{
    // Proceed with xmlHttp usage.
}

Edit

Note I tend to avoid the old ProgID "Microsoft.XMLHTTP" in favour of the one I have used because this later ProgID has a more predictable behaviour and is ever so slightly more secure. However if you want wider compatiblity with really old Windows machines (I'm talking out-of-support stuff) then you could use the older one in your specific case.


var xmlHttp=new(window.ActiveXObject?ActiveXObject:XMLHttpRequest)('Microsoft.XMLHTTP');

0

精彩评论

暂无评论...
验证码 换一张
取 消