当使用 .ashx 地址时出现 javascript 错误:myxmlhttprequest.open 不是函数
我无法访问 SayHello.ashx 并且收到此 javascript 错误:
httpReq.open 不是一个函数。
但是当我使用 aspx 页面来回答 xmlhttprequest 时,代码工作正常:
httpReq.open("POST", "SayHello.aspx");
问题出在哪里?问题可能出在我的 .ashx 文件上吗?
function callASHX() {
var httpReq=XMLHttpRequest();
var x = document.getElementById('txtName').value;
var sendStr = "user_id=" + x;
httpReq.open("POST", "SayHello.ashx");
httpReq.onreadystatechange = XMLHttpRequestCompleted;
httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpReq.send(sendStr);
}
// initialize XMLHttpRequest object
function XMLHttpRequest() {
var httpReq;
try {
// Opera 8.0+, Firefox, Safari
httpReq = new XMLHttpRequest();
}
catch (e) {
// IEBrowsers
try {
httpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
return false;
}
}
}
return httpReq;
}
i cant access to SayHello.ashx and i got this javascript error :
httpReq.open is not a function.
but the code work fine when i use an aspx page for answer xmlhttprequest :
httpReq.open("POST", "SayHello.aspx");
where is the problem? can the problem be from my .ashx file ?
function callASHX() {
var httpReq=XMLHttpRequest();
var x = document.getElementById('txtName').value;
var sendStr = "user_id=" + x;
httpReq.open("POST", "SayHello.ashx");
httpReq.onreadystatechange = XMLHttpRequestCompleted;
httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpReq.send(sendStr);
}
// initialize XMLHttpRequest object
function XMLHttpRequest() {
var httpReq;
try {
// Opera 8.0+, Firefox, Safari
httpReq = new XMLHttpRequest();
}
catch (e) {
// IEBrowsers
try {
httpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
return false;
}
}
}
return httpReq;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有分配
XMLHttpRequest()
的返回对象。试试这个:
You're not assigning the return object of
XMLHttpRequest()
.Try this: