当Ajax调用时,pairs key = value按每个符号代码分割

时间:2012-12-05 04:56:41

标签: javascript jquery ajax fullcalendar

无法理解为什么我的key = value对转换为符号,而在我的ajax GET调用中我有:

GET /admin_schedule/get_schedule_db/?0=%5B&1=o&2=b&3=j&4=e&5=c&6=t&7=+&8=O&9=b&10=j&11=e&12=c&13=t&14=%5D&15=%22&16=%26&17=t&18=e&19=a&20=c

而不是:

GET /admin_schedule/get_schedule_db/?teacherArray[]=128&teacherArray[]=134...

我的代码:

var eventss = ''; 
$("input[type='checkbox'][name='teacher']").each( function() { 
    if(this.checked) {
        eventss += "&teacherArray[]=" + $(this).attr("value");
    }
});
events1.data += eventss;

fullcalendar eventSources的ajax:

var events1 = {
    url: '/admin_schedule/get_schedule_db/',
    type: 'GET',
    data: {sch_teacher_id: (sch_teacher_id) ? sch_teacher_id : $('.teacher').val() },
    success: function (response) {
        return response;
    }
};

然后使用事件

获取fullcalendar
eventSources: [ 
            events1,
            events2,
            events3 
        ],

2 个答案:

答案 0 :(得分:3)

将字符串与对象连接几乎不是一个好主意,因为Object#toString始终返回"[object Object]"。除非在对象中覆盖toString,否则在连接对象之前,该对象将被转换为字符串作为此字符串(意味着其内容丢失)。此外,结果字符串不是有效的查询字符串。

而不是

eventss += "&teacherArray[]=" + $(this).attr("value");
...
events1.data += eventss;

尝试在teacherArray

中创建一个空的data
events1.data.teacherArray.push($(this).attr("value"));

另请考虑使用$("#my-form").serialize()

答案 1 :(得分:0)

我解决了这个问题! (Jan Dvorak是您的意见!)。

1。变量现在是一个数组:

var sch_teacher_id = new Array("<?php echo $sch_teacher_id; ?>");

2。每次循环前清空数组:

$('.teacher').change(function (event) {
    events1.data.sch_teacher_id = [];
    events2.data.sch_teacher_id = [];
    events3.data.sch_teacher_id = [];

    if($(this).val()) { 

        $("input[type='checkbox'][name='teacher']").each( function() {  
            if(this.checked) {                          
                events1.data.sch_teacher_id.push($(this).attr("value"));     
                events2.data.sch_teacher_id.push($(this).attr("value"));          
                events3.data.sch_teacher_id.push($(this).attr("value"));
            }   
        });  

    }
    $calendar.fullCalendar('refetchEvents');
});