when use .ashx adress got javascript error : myxmlhttprequest.open is not a function
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=XMLHttpRequ开发者_JS百科est();
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;
}
You're not assigning the return object of XMLHttpRequest()
.
Try this:
function callASHX()
{
var httpReq = XMLHttpRequest();
....
if (httpReq) //potentially this is 'false'!
{
httpReq.open("POST", "SayHello.ashx");
....
}
精彩评论