简体   繁体   English

Ajax调用重新加载页面而不调用回调函数

[英]ajax call reloading page and not calling callback function

I searched and seen many asking similar questions, but none of them answer/help my issue. 我搜寻了许多类似的问题,但没有一个回答/帮助我的问题。

Below is the js code I am using to do an ajax call. 以下是我用来进行Ajax调用的js代码。 The post works, when I walk thru the code using firebug, I can see the return message in the response. 这篇文章有用,当我使用Firebug浏览代码时,我可以在响应中看到返回消息。 But the call back function is not called. 但是不调用回调函数。 The test line is ran and the page gets reloaded. 测试行已运行,页面已重新加载。 what am I missing to get the call back function to work and the page not to refresh? 我缺少使回调函数正常工作且页面无法刷新的原因? also looking in firebug, I can see under this.xmlhttp: onreadystatechange = callback(); 还在看萤火虫,我可以在this.xmlhttp下看到:onreadystatechange = callback();

Here is the code: 这是代码:

function testajax() {
    params = 'action=testing';
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST", "afs_controller", true);
    xmlhttp.onreadystatechange = callBack;
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    //just test line;
    msg = 'testing;';
}

function callBack() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        handleReturn(xmlhttp.responseText);
    } else {
        failedCallBack(xmlhttp.responseText);
    }
}

function handleReturn(responseText) {
    document.getElementByID(displayDiv).innerHTML = responseText;
    document.getElementByID(displayDiv).style.display = 'block';
}

function failedCallBack(responseText) {
    var msg;
    msg = responseText;
    msg = msg + ' here';
    //handle here;
}

No were in my post do I mention JAVA, all the code shown is javascript. 我的帖子中没有提到Java,所有显示的代码都是javascript。

I did not post the JAVA code, shouldn't be needed. 我没有发布JAVA代码,因此不需要。 I am getting the response back just fine. 我得到的回复很好。 Using firebug, I can see were the readystate changes after the send() and can see the returned text in firebug's console. 使用firebug,我可以看到send()之后ready状态发生了变化,并且可以在firebug的控制台中看到返回的文本。

Not sure why you are confused. 不知道为什么你会感到困惑。 Maybe you should re-read the post. 也许您应该重新阅读该帖子。

Created a fiddle which works just fine. 创建了一个小提琴 ,效果很好。 So not sure what is going wrong at your side. 因此,不确定您这边出了什么问题。

Another thing: try to define your variable before using then: 另一件事:在使用then之前尝试定义变量:

function testajax() {
    var params = 'action=testing';
    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST", "/echo/json", true);
    xmlhttp.onreadystatechange = callBack;
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
}

excellent example page for XMLHttpRequest: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php XMLHttpRequest的出色示例页面: http : //www.openjs.com/articles/ajax_xmlhttp_using_post.php

Just asking: why not use jQuery ? 只是问:为什么不使用jQuery

Example: 例:

$.ajax({
    type: 'POST',
    url: 'afs_controller',
    data: { 
        'action': 'testing'
    },
    success: handleReturn,
    error: failedCallBack
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM