weixin_33705053 2011-08-14 00:42 采纳率: 0%
浏览 23

Javascript AJAX类问题

Hi I'm not sure exactly how to describe this problem, so I'm hoping the code will explain my problem. The php script ( phppage.php ) called by the ajax function does nothing more than echo hello world.

When the alert('a') line is left in the recall function withing the ajax function, then the code works as expected, and the final line pops up the message "hello world". However then the alert('a') line is commented out, then the final line does not give "hello world", but the value 13, as is set in the constructor function.

I'm trying this on firefox 3.6.18

Any help would be gratefully appreciated.

function A() {
    this.b = 13;
    function finish(context,response) {
        context.b = response;
    }
    ajax(finish,this);
}

A.prototype = {
    constructor: A
}

function ajax(callback,context) {
    var http = new XMLHttpRequest();
    var url = "phppage.php";
    http.open("GET", url, true);
    http.onreadystatechange = recall;
    function recall() {
        alert('a');
        if(http.readyState == 4 && http.status == 200) {
            callback(context,http.responseText);
        }
    }
    http.send(null);
}

var d = new A();
alert(d.b);
  • 写回答

1条回答 默认 最新

  • weixin_33682790 2011-08-14 01:34
    关注

    I see a couple of problems with your code. First one is with your finish function, what you are doing with the context variable is very poor style and Javascript has a feature called closures that will clean things up for you. You constructor should look more like this:

    function A() {
        this.b = 13;
        //this is how you should be storing contexts
        var that = this;
        function finish(response) {
            that.b = response;
        }
        ajax(finish);
    }
    

    finish will have access to that even after the constructor A has returned. Also you don't need that prototype stuff you have under A either. It is literally doing nothing.

    Finally we can tidy up your AJAX function a bit so it looks like this:

    function ajax(callback) {
        var http = new XMLHttpRequest();
        var url = "phppage.php";
        http.open("GET", url, true);
        http.onreadystatechange = function() {
            alert('a');
            if(http.readyState == 4 && http.status == 200) {
                callback(http.responseText);
            }
        }
        http.send(null);
    }
    

    Note, this is all untested code so no guarantees it will work but i think it will point you in the right direction.

    EDIT:

    Now that I look at your code I realize that it probably is working its just that you are calling ajax asynchronously so it returns right away and you are echoing the value of b before you get it back.

    might want to try:

    http.open("GET", url, false);
    

    (but all of my comments above should still be considered because they will really clean things up a bit)

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能