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

    评论

报告相同问题?