嵌套 getJson() & PHP PDO

发布于 2024-11-02 21:25:51 字数 1492 浏览 1 评论 0原文

我已经查看了与此相关的其他问题和答案,但似乎无法理解它...

我有一个 javascript 函数:

function getStates(theDiv){
var stateGroupData;
var stateData;

var theGHtml = "";

var theHtml = "<h4>MyPage</h4>";
theHtml = theHtml+"<h5>select a state...</h5>";

$.getJSON("getStateGroups.php", function(data) {
    stateGroupData = data;
    theHtml = theHtml+"<ul>";

    $.each(stateGroupData, function(i,jsonData) {
        theHtml = theHtml+"<li><a href='#"+jsonData.groupName+"'>"+jsonData.groupID+"</a></li><br/>";

        var theSQL = "getStates.php?gid="+jsonData.groupName;

        theGHtml = theGHtml+"<div id='"+jsonData.groupName+"'>";

        $.getJSON(theSQL, function(data2) {   
            stateData = data2;

            $.each(stateData, function(i,jsonData2) {
                                       alert(jsonData2.stateName);
                theGHtml = theGHtml+"<span sname='"+jsonData2.stateName+"' lat='"+jsonData2.centerLat+"' lon='"+jsonData2.centerLon+"' zom='"+jsonData2.zoom+"'>"+jsonData2.stateName+"</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            });

        });
        theGHtml = theGHtml+"</div>";

    });

    theHtml = theHtml+"</ul>";
});

theDiv.html = theHtml+theGHtml;
}

第二个(即嵌套) getJson 不返回任何内容...两个 PHP 文件只需使用 PDO 从 SAME 表请求数据即可。我在每个文件中运行 SQL 没有任何问题,因此 SQL 看起来没问题。

这是调用 getJson 的同步与异步问题吗?

I have looked at other questions and answers regarding this, but can't seem to wrap my head around it...

I have a javascript function:

function getStates(theDiv){
var stateGroupData;
var stateData;

var theGHtml = "";

var theHtml = "<h4>MyPage</h4>";
theHtml = theHtml+"<h5>select a state...</h5>";

$.getJSON("getStateGroups.php", function(data) {
    stateGroupData = data;
    theHtml = theHtml+"<ul>";

    $.each(stateGroupData, function(i,jsonData) {
        theHtml = theHtml+"<li><a href='#"+jsonData.groupName+"'>"+jsonData.groupID+"</a></li><br/>";

        var theSQL = "getStates.php?gid="+jsonData.groupName;

        theGHtml = theGHtml+"<div id='"+jsonData.groupName+"'>";

        $.getJSON(theSQL, function(data2) {   
            stateData = data2;

            $.each(stateData, function(i,jsonData2) {
                                       alert(jsonData2.stateName);
                theGHtml = theGHtml+"<span sname='"+jsonData2.stateName+"' lat='"+jsonData2.centerLat+"' lon='"+jsonData2.centerLon+"' zom='"+jsonData2.zoom+"'>"+jsonData2.stateName+"</span>        ";
            });

        });
        theGHtml = theGHtml+"</div>";

    });

    theHtml = theHtml+"</ul>";
});

theDiv.html = theHtml+theGHtml;
}

The second (ie. nested) getJson does not return any thing... Both PHP files just use PDO to request data from the SAME table. I run the SQL in each file without any issues, so the SQL seems OK.

Is this an sync v. async issue with the calls to getJson?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-11-09 21:25:51

这是同步还是异步问题
对 getJson 的调用?

大概。我认为这是你的问题:

stateData = data2;

尝试将其更改为:

var stateData = data2;

第一个设置全局变量。第二个设置该函数的本地变量。

您可能会受益于重构整个过程,这样您只需要进行一次 AJAX 调用。看起来你正在拉动与某个群体相关的个人。您可以通过单个脚本在服务器上获得更好的性能,该脚本可以在需要时返回与该组关联的人员,但否则仅返回该组。

请记住,每个 AJAX 调用都会对您的服务器造成又一次打击。

Is this an sync v. async issue with
the calls to getJson?

Probably. I think this is your problem:

stateData = data2;

Try changing that to:

var stateData = data2;

The first one sets a global variable. The second one sets a variable that is local to that function.

You might benefit from refactoring this whole process such that you only need to make one AJAX call. It looked like you were pulling individual people associated with a group. You'd get better performance on the server from a single script which can, when needed, return people associated with the group but otherwise just returns the group.

Remember, every AJAX call is another hit to your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文