本帖最后由 tiancaolin 于 2010-12-02 11:38:22 编辑

解决方案 »

  1.   

    扩展需求:
    1.AjaxHelper.getScript()方法中的callback方法 我想外界传递参数进去,要怎么传递。
    2.我在html中创建<script></script>,又该如何将远程服务器上的 信息在callback方法中处理
    =======================
    帖子描述错了
    重新更正需求:
    就是怎么在callback方法中调用远程服务器的那些数据。请举例下,我尝试不可以
      

  2.   

    AjaxHelper.getScript({
              url:"http://data.585.com/opendatahandler.ashx?action=klinelistflashdatajson",
              data:"keyword=sh600036",
              callback:function(e){
                 // 看看e有没有东西
                 alert("执行完毕");
              }     
            });
      

  3.   

    为方便调试,我整理下完整可调试代码,快刀斧,你帮我看看下
    js代码: var AjaxHelper = {
            charset: "utf-8",
            data: "",
            url: location.href,
            msg: "",
            callback: function () { },
            getHttpRequest: function () {
                var xmlhttp = null;
                if (window.ActiveXObject) {
                    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                    catch (ex) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
                }
                else if (window.XMLHttpRequest) {
                    try { xmlhttp = new XMLHttpRequest(); }
                    catch (ex) { xmlhttp = window.createRequest(); }
                    if (xmlhttp.overrideMimeType)
                        xmlhttp.overrideMimeType('text/xml');
                }
                return xmlhttp;
            },
            getRandomNumber: function () {
                var date = new Date();
                return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()) + Math.random().toString().replace(".", "");
            },        getScript: function (ajaxsettingcfn, callback) {            if (typeof ajaxsettingcfn == "function") {
                    callback = ajaxsettingcfn;
                    ajaxsettingcfn = null;
                }
             
                if (ajaxsettingcfn) {
                    if (ajaxsettingcfn.url) { this.url = ajaxsettingcfn.url; }
                    if (ajaxsettingcfn.charset) { this.charset = ajaxsettingcfn.charset; }
                    if (ajaxsettingcfn.data) {
                        if (ajaxsettingcfn.data.substr(ajaxsettingcfn.data.length - 1) == "&") {
                            this.data += ajaxsettingcfn.data;
                        }
                        else {
                            this.data += "&" + this.data;
                        }
                        this.url += this.data;
                    }
                }            var head = document.getElementsByTagName("head")[0] || document.documentElement;
                var script = $C("script");
                script.charset = this.charset;
                script.id = "STOCKWARNSCRIPTTAG"
                script.type = "text/javascript";
                script.src = this.url + "&rn=" + this.getRandomNumber();            head.appendChild(script);
                document.body.appendChild(script);
                script.onload = script.onreadystatechange = function () {
                    if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {
                        if (callback) {
                            callback();
                        }
                        script.onload = script.onreadystatechange = null;
                        $G("STOCKWARNSCRIPTTAG").parentNode.removeChild($G("STOCKWARNSCRIPTTAG"));
                    }
                };
            }
        }
     html代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>股票预警测试页面</title>
        <style type="text/css"> 
        .menu-post {
        background:none repeat scroll 0 0 #0673B5;
        border:2px solid #A9DAF7;
        padding:1px;
        }
     
        .menu {
        background:none repeat scroll 0 0 #FFFFFF;
        border:1px solid #A9DAF7;
        position:absolute;
        }
        
        .menu-post .menu-b {
       background:none repeat scroll 0 0 #F8F8F8;
        border:1px solid #A9DAF7;
       }   .tac {
       text-align:center;
       }
        .menu-b .h {
    border-bottom:1px solid #9DB3C5;
    }
     
    .h {
    background:none repeat scroll 0 0 #1395E0;
    border-bottom:1px solid #9DB3C5;
    color:#FFFFFF;
    line-height:30px;
    padding:0 10px;
    text-align:left;
    }.h span a, .h span {
    color:#336699;
    }.h span {
    font-weight:normal;
    }
            
        </style>
       <script src="Scripts/StockWarn.js" language="javascript" type="text/javascript"></script>-->
      
        <script language="javascript" type="text/javascript" >
    //远程URL地址:http://data.585.com/opendatahandler.ashx?action=openstockdata&source=stockquotation_only&keyword=sh600036
            window.onload = function () {
                   var stockid = "sh600036";
                AjaxHelper.getScript({
                    url: "http://data.585.com/opendatahandler.ashx?action=openstockdata",
                    data: "source=stockquotation_only&keyword=" + stockid 
                }, function () {                alert(serverState); //这里会报错,没定义,但你打开远程URL地址是有的,重点问题是解决这个
                    alert("执行完毕:" + stockid);            });
             }           </script>
     
    </head>
    <body>
    <a href="javascript:;">sh600031</a>
    <a href="javascript:;">测试1</a>
    <a href="javascript:;">600031</a>
    <a href="javascript:;" id="test222" >123</a> 
    </body>
    </html>
      

  4.   

    另外IE可以  有效触发  if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {
                        if (callback) {
                            callback();
                        }
    里面的callback方法;但firefox好像不行,不知道什么问题,疯掉了真TM的敲一个兼容问题就出来
      

  5.   

    if (typeof $G == 'undefined')
        $G = function (id) {
            return document.getElementById(id);
        };
    if (typeof $C == 'undefined')
        $C = function (t) {
            return document.createElement(t);
        };
      

  6.   

    AjaxHelper.getScript() 这个执行完了有没返回结果?
      

  7.   

    暴汗啊。
      this.data += "&" + this.data;
    这个地方写错了。。哈哈,每次快刀斧提的我去调试那方面都很有收获现在IE已经彻底解决了但FireFox似乎无法触发   
     if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {
    无法触发这里面的方法}
      

  8.   

      this.data += "&" + this.data;   
    应该写成  this.data += "&" + ajaxsettingcfn.data;   
      

  9.   

     if(typeof(callback)=='function'){
            if( document.all ){
               script.onreadystatechange = function(){
                    if(/(complete|loaded)/.test(this.readyState)){
                          callback(); script.onreadystatechange = null; script.parentNode.removeChild(script); 
                     }};
           }else{
                 script.onload = function(){  callback(); script.onload = null; script.parentNode.removeChild(script); };
           }
        }