javascript - 将此 javascript 对象从 jQuery 传递到 PHP?

标签 javascript php jquery ajax arrays

这个问题源于this thread .

我已遵循下面的答案,但在将对象传递到 PHP 时遇到问题。我认为这只是一个小问题,但我找不到它。

我的ajax调用

 $('.actResult').click(function() {
        var result = {};
        $('.actResult tr').each(function(){
            var $tds = $(this).find('td');
            result[$tds.eq(0).html()] = $tds.eq(1).text();
        });
        console.log(result);
        $.ajax({
            type: 'get',
            url: 'userpage.php',
            data: result
        });
        $('.FindResults').dialog("close");
    });

在 userpage.php 中,我使用这个:

echo '<div id="data"><pre>', var_dump($_GET), '</pre></div>';

可能我可能需要使用 stringify 或 json_decode,但是 this source告诉我进行 ajax 调用就足够了。

输出给了我一个

array(0){
}

这很奇怪。该数组会打印到控制台中,以便正确生成。控制台还告诉我ajax执行成功。我使用 $_GET 只是因为 $_POST 已经有很多变量,因此检查 $_GET 是否有此请求会更容易。

更新:

从下面的评论来看,当查询成功时,ajax 调用不会执行任何操作。所以我改变了调用:

$.ajax({
            type: 'get',
            url: 'userpage.php',
            data: result,
            success: function(){
                $('#data').text( data );
            }
        });

还有 PHP

echo '<input type="text" id="data" /><pre>', var_dump($_GET), '</pre>';

我尝试使用 div 而不是文本框。结果仍然是 array(0){}

最佳答案

$.ajax({ 
   type       : "GET",  
   data       : { result : JSON.stringify(result) },  
   dataType   : "html",  
   url        : "userpage.php",  
   beforeSend : function(){ },
   success    : function(data){ console.log( data ) },
   error      : function(jqXHR, textStatus, errorThrown) { }
});

在你的PHP中

echo '<div id="data"><pre>'. $_GET["result"] .'</pre></div>';

关于javascript - 将此 javascript 对象从 jQuery 传递到 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645323/

相关文章:

javascript - Jquery/Javascript 日期选择器概念插件

php - PDO 多重查询理解问题

php - 在 php session 中存储安全用户数据的最佳方法是什么?

javascript - 具有嵌套子菜单的 jQuery 导航项应在单击时打开

javascript - 超时后重定向到其他 View

php - 捕获重复模式的正则表达式问题

javascript - fullpage.js : the height cuts the text

javascript - TinyMCE React 编辑器抛出 'Cannot set property ' onload' of null'

javascript - Contenteditable div 删除 <font>

javascript - "value"在 d3 力定向图中代表什么