Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让javascript单例通过$.post返回数据?_Javascript_Jquery_Ajax - Fatal编程技术网

如何让javascript单例通过$.post返回数据?

如何让javascript单例通过$.post返回数据?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想在singletondp.DatapodManager上创建一个方法,以便它通过$.post加载一次数据,然后我可以通过调用singleton上的方法来使用该数据 以下代码的输出为: 111 222 test data 要获得dp.DatapodManager.loadDirective(),我必须做什么将文本文件的内容添加到div#content <html> <head> <title>test load</title

我想在singleton
dp.DatapodManager
上创建一个方法,以便它通过$.post加载一次数据,然后我可以通过调用singleton上的方法来使用该数据

以下代码的输出为:

111

222
test data
要获得dp.DatapodManager.loadDirective(),我必须做什么将文本文件的内容添加到
div#content

<html>
    <head>
        <title>test load</title>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('div#content').append('<p>1111</p>');
                $('div#content').append('<p>222</p>');
                $('div#content').append(dp.DatapodManager.getTestData());
                dp.DatapodManager.loadDirectly(function(html) {
                    $('div#content').append(html);
                });
            });

            var dp = dp || {
                VERSION : '0.00.05'
            };

            dp.DatapodManager = (function() {
                return {
                    loadDirectly: function(callback) {
                        dp.qsys.loadDataFromExternalWebsite(function(stringBlock) {
                            var lines = dp.qstr.convertStringBlockToLines(stringBlock);
                            var html = dp.qstr.appendBrToLines(lines);
                            callback(html); //never executes, cannot set breakpoint here in firebug
                        });     
                        callback('<p>this is returned</p>');
                    },
                    getTestData: function() {
                        return 'test data';
                    }
                }
            }());

            dp.qsys = {
                loadDataFromExternalWebsite : function(callback) {
                    url = 'http://localhost/webs/dpjs/data/data.txt';

                    var json = '';
                    (function() {
                        var json = null;
                        $.post(url, {}, function(jsonString) {
                            callback(jsonString);
                        });

                        return json;
                    })();
                }
            };


            dp.qstr = {
                convertStringBlockToLines: function(block, trimLines) {
                    var trimLines = trimLines || true;
                    var lines = block.split(dp.qstr.NEW_LINE());
                    if(trimLines && lines.length > 0) {
                        for(x=0; x<lines.length; x++) {
                            lines[x] = lines[x].trim(); 
                        }
                    }
                    return lines;
                },

                NEW_LINE: function() {
                    return '\r\n';
                }, 

                appendBrToLines: function(lines) {
                    var r = '';
                    if(lines.length > 0) {
                        for(x=0; x<lines.length; x++) {
                            r += lines[x] + '<br/>';
                        }
                    }
                    return r;
                }
            };


        </script>
    </head>
<body>
    <div id="content"></div>

</body>
</html>


试验负荷
$(文档).ready(函数(){
$('div#content')。追加('p>1111

'); $('div#content')。追加('p>222

'); $('div#content').append(dp.DatapodManager.getTestData()); dp.DatapodManager.loadDirectly(函数(html){ $('div#content').append(html); }); }); var-dp=dp | |{ 版本:“0.00.05” }; dp.DatapodManager=(函数(){ 返回{ 直接加载:函数(回调){ dp.qsys.loadDataFromExternalWebsite(函数(stringBlock)){ 变量线=dp.qstr.convertStringBlockToLines(stringBlock); var html=dp.qstr.appendbrtoline(行); 回调(html);//从不执行,无法在firebug中在此设置断点 }); 回调(“这是返回的”

”); }, getTestData:函数(){ 返回“测试数据”; } } }()); dp.qsys={ loadDataFromExternalWebsite:函数(回调){ url='1〕http://localhost/webs/dpjs/data/data.txt'; var json=''; (功能(){ var=null; $.post(url,{},函数(jsonString){ 回调(jsonString); }); 返回json; })(); } }; dp.qstr={ convertStringBlockToLines:函数(块、修剪线){ var trimLines=trimLines | | true; var LINE=block.split(dp.qstr.NEW_LINE()); 如果(修剪线条和线条长度>0){ 对于(x=0;x 0){
对于(x=0;xYou不能。相反,将函数作为“回调”参数传递,并让该函数根据需要执行工作。好的,我将函数作为回调参数传递,并让函数fill
div\content
(代码已在上面更改)但它仍然给我相同的输出。你能在Chrome dev tools网络视图中打开它吗?看起来响应无效。对,我现在删除了
var text
。第一个不起作用的部分是正在执行第二个回调(
这是返回的
),而不是第一个回调(
html
),如何获取
loadDirectly
以返回它从
dp.qsys.loadDataFromExternalWebsite
获取的html值。第二个问题是我在帖子中遇到的错误200,url是有效的并返回文本。这真的很奇怪。我已经粘贴了你的代码并在我的本地主机上进行了测试:唯一的区别是文件加载了b现在,y$.post只是一个简单的文本文件,一切正常:该文件的内容被处理并附加到div中。