获取 getXMLHTTP 未定义错误 [英] Getting a getXMLHTTP is not defined error
本文介绍了获取 getXMLHTTP 未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 javascript:
Here is my javascript:
<script type="text/javascript">
function getState(countryId)
{
var strURL="findState.php?country="+countryId;
var req = getXMLHTTP();
//ERROR is right here
//UNCAUGHT REFERENCE ERROR: getXMLHTTP IS NOT DEFINED
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
document.getElementById('statediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
它正在被调用:
<tr>
<td width="150">Country</td>
<td width="150"><select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"><option>Select Country</option><option value="1">USA</option><option value="2">Canada</option> </select></td>
</tr>
为了安全起见,这是我的标题,也许我做错了?
Just to be safe, this is my header, maybe I am doing this incorrectly?
<head>
<link rel="stylesheet" type="text/css" href="view/css/application.css" />
<script type="text/javascript" src="view/javascript/application.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>
另一个注意事项,我正在处理 this 示例,他使用的是 mysql 服务器,我使用 ODBC 连接到 Access 数据库.我不会为此使用 xmlHTTP 吗?老实说我不知道.
Another note, I am working off this example and he is using a mysql server, I am using ODBC to connect to an Access database. Would I not use xmlHTTP for this? I honestly don't know.
推荐答案
你的 getXMLHTTP() 应该是这样的:
Your getXMLHTTP() should look like this :
<script type="text/javascript">
function getXMLHTTP() {
var x = false;
try {
x = new XMLHttpRequest();
}catch(e) {
try {
x = new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1) {
x = false;
}
}
}
return x;
}
function getState(countryId){
var strURL="findState.php?country="+countryId;
var req = getXMLHTTP();
if (req){
req.onreadystatechange = function(){
if (req.readyState == 4){
if (req.status == 200){
document.getElementById('statediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
这篇关于获取 getXMLHTTP 未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文