简体   繁体   中英

Cannot pass object to php with js ajax

I create js object:

   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);

I need pass taskTypes object to php. In console I get right object: https://clip2net.com/s/3RZXM4b

Then I try pass object:

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

        }, "json");

Php file:

 show($_REQUEST['taskTypes']);

But In php I just get it (cant get tags key): https://clip2net.com/s/3RZXQaK

How can I fix it? Thanks.

Instead of saying taskTypes[typeId]["tags"] = {}; taskTypes[typeId].tags = currentTags; taskTypes[typeId]["tags"] = {}; taskTypes[typeId].tags = currentTags; try this taskTypes["tags"] = {}; taskTypes.tags = currentTags; taskTypes["tags"] = {}; taskTypes.tags = currentTags; because in your code your actually adding a tags attribute to your first array, which does not get properly parsed by jQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM