【问题标题】:Pass a return value from php to js将返回值从 php 传递给 js
【发布时间】:2016-09-19 21:00:51
【问题描述】:

我有 3 个文件 main.php、action.js 和 ajax.php,我成功地将点击一些 div 的内容从 main.php 更改为一些 ajax.php,并在我的 javascript 文件中调用了 ajax。它看起来像这样:

var value = $(this).attr("id");

$.ajax({
    type: 'get',
    url: "ajax.php",
    data: {
        auto_value: value
    },
    success: function(response) {
        $('.gridnr1, .textnr1').fadeOut(400, function(){
            $('.textnr2, .gridnr2').fadeIn(400);
        });

        var newtextcaption = $(response).filter('#newtextcaption').html();
        var box = $(response).filter('#box').html();

        $('#textnr2').html(newtextcaption);
        $('#gridnr2').html(box);

        for (var i=0; i<VALUE_FROM_AJAXphp; i++) {
            DO SOMETHING WITH i;
        }
    });

现在我需要从我的 action.js 中的 ajax.php 中的函数返回值,因为我想迭代直到这个值(参见上面的代码)。 如何将此值从 ajax.php 传递给 action.js。 我很困惑我需要什么来获得价值 ajax?,json?还是别的什么?

谢谢。

【问题讨论】:

  • 您的牙套没有正确匹配。

标签: javascript php jquery json ajax


【解决方案1】:

在success函数中,response就是你从PHP中得到的。所以发送 JSON 是最简单的,因为你的响应只是一个对象。

假设 ajax.php 返回这个 JSON

{
    "newtextcaption": "whatever the text is",
    "boxhtml": "whatever your box html is",
    "AjaxValue": 4389489473289
}

那么你的成功函数应该是

 success: function(response) {
                $('.gridnr1, .textnr1').fadeOut(400, function(){
                    $('.textnr2, .gridnr2').fadeIn(400);
                });

                var newtextcaption = response.newtextcaption;
                var box = response.boxhtml;

                $('#textnr2').html(newtextcaption);
                $('#gridnr2').html(box);

                for (var i=0; i<response.AjaxValue; i++) {
                     DO SOMETHING WITH i;
                }

【讨论】:

    猜你喜欢
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多