开发者

Ajax Javascript Scope Issues

I am having a javascript scope issue I want to take the responce text from the ajax call and place it into a global variable. Then process the JSON in another function here is my code.

var JSONDATA = "not gatherd";
var ajaxCalls = (function(){

                var ajaxer = {                  

                    defaults:{
                        url:"test.php",
                        DirectHTML: true,
                        element:"#ajaxerizer"               
                    },                  
                    setup:function(setup){              
                        var defaulLengther = this.defaults

                        for (var key in defaulLengther)
                        {
                            if(setup.hasOwnProperty(key))
                            {   
                                this.defaults[key] = setup[key];

                            }
                        }

                        if(this.defaults.DirectHTML === false)
                        {

                            if (window.XMLHttpRequest) {
                                this.ajaxRequester = new XMLHttpRequest();
                            }
                            if (window.ActiveXObject) {
                                this.ajaxRequester = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            this.ajaxRequester.open('POST', this.defaults.url, true);
                            this.ajaxRequester.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                            this.ajaxRequester.send();
                        }
                        this.callIt();
                    },                  


                    callIt:function(){
                        if(this.defaults.DirectHTML === true)
                        {
                            $(this.defaults.element).load(this.defaults.url);
                        }

                        if(this.defaults.DirectHTML === false)
                        {       

                            this.ajaxRequester.onreadystatechange = function(){
                                if (this.readyState == 4) {
                                    //This is where I have trouble
                                    alert(this.responseText);
                                    JSONDATA = this.responseText;//This is the data I want to process and use
                                    alert(JSONDATA);
                                }

                            }
                        }

                    }


                }
                return ajax开发者_如何学Cer

            })();

Here is the Index

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
        <script src="simpleHtmlAjax.js"></script>

    </head>
    <body>
        <div id="ajaxerizer">   
            <script>
                ajaxCalls.setup({
                            url:"json.php",
                            DirectHTML: false,
                    });
                    alert(JSONDATA);
            </script>
        </div>

    </body>
</html>

and the JSON data

<?php
$json = array("one" => 1,"two" => 2,"three" => 3,"four" => 4);
echo json_encode($json);
?>

Thank you for any help.


You're already including jQuery in your HTML, so why not just use jQuery's AJAX helper functions to automatically process the JSON data?

$.post("json.php", function (data) {
    // do something with data, which should be a plain JS object:
    // {"one":1, "two":2, "three":3, "four":4}
}, "json");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜