weixin_33737134 2014-11-24 18:23 采纳率: 0%
浏览 137

通过AJax传递变量

Im on project to make an app with codeigniter but i got stuck, when i want to pass same variable outside field via ajax into controller i got error the variable is not defined.

this is a example.

  $("#form_status_update").submit(function() {
          var date = new Date().toString();`
                    $.ajax({
                    type: 'POST',
                    url: "<?php echo base_url()?>socialcontroller/setdate",
                    data:date,
                    success: function(data) {
                       window.location.href = "<?php echo base_url()?>socialcontroller/ssetdate";

                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError); //throw any errors
                    }
                });
            });

after passing some var i want to insert into my database. and my question is how to pass a variable not field into controller via ajax thanks, i already search on google but i didnt geting the right answer :) `

  • 写回答

4条回答 默认 最新

  • ~Onlooker 2014-11-24 18:31
    关注

    The line data:date, is wrong. You are passing up a number on the querystring and not a key/value pair.

    It needs to be

    data: {date : date},
    

    or

    data: "date=" + encodeURIComponent(date),
    

    From jQuery Docs:

    data

    Type: PlainObject or String or Array

    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码