【问题标题】:Cannot pass object to php with js ajax无法使用 js ajax 将对象传递给 php
【发布时间】:2018-02-18 10:07:05
【问题描述】:

我创建 js 对象:

   var taskTypes = {};


    $(".task_types_tags_category").each(function () {
        var name = $(this).attr("name");
        name = name.replace("[]","");
        var typeId = $(this).val();
        var currentTagsCount =0;
        var currentTags = [];
       if($(this).is(":checked")){
           taskTypes[typeId]={};
           taskTypes[typeId]=[typeId];
       }
       $('input[name="'+name+'[task_types_tags][]"]').each(function () {
            if($(this).is(":checked")){
                currentTags.push($(this).val());
                ++currentTagsCount;
            }
        });
       if(currentTagsCount >0){
           taskTypes[typeId]={};
           taskTypes[typeId]=[typeId];
           taskTypes[typeId]["tags"]={};
           taskTypes[typeId].tags = currentTags;
       }
    });

    console.log(taskTypes);

我需要将 taskTypes 对象传递给 php。 在控制台中,我得到了正确的对象: https://clip2net.com/s/3RZXM4b

然后我尝试传递对象:

$.post(sJSUrlSaveLeadNote, {taskTypes:taskTypes},
        function (data) {

        }, "json");

php 文件:

 show($_REQUEST['taskTypes']);

但在 php 中我只是得到它(无法获取标签键): https://clip2net.com/s/3RZXQaK

我该如何解决?谢谢。

【问题讨论】:

  • JSON.stringify( taskTypes ) ?

标签: javascript php jquery ajax object


【解决方案1】:

不要说taskTypes[typeId]["tags"] = {}; taskTypes[typeId].tags = currentTags;,试试这个taskTypes["tags"] = {}; taskTypes.tags = currentTags;,因为在你的代码中,你实际上是在你的第一个数组中添加了一个标签属性,它没有被jQuery正确解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多