简体   繁体   English

无法从jQuery Ajax请求访问数据,返回空数组

[英]Cannot access data from jQuery Ajax request, returns empty array

I have a form that is called via the fancybox plugin login example. 我有一个通过fancybox插件登录示例调用的表单。

Here is the code I have: 这是我的代码:

Form: 形成:

<form method="post" action="" id="events_form">
    <p class="clearfix"><label for="Name">Name:</label> <input type="text" name="Name" id="Name" /></p>
    <p class="clearfix"><label for="Company">Company:</label> <input type="text" name="Company" id="Company" /></p>
    <p class="clearfix"><label for="Email">Email:</label> <input type="text" name="Email" id="Email" /></p>
    <p class="clearfix"><label for="Tel">Tel:</label> <input type="text" name="Tel" id="Tel"/></p>
    <p class="clearfix"><input type="submit" value="Submit details" /></p>
 </form>

JavaScript / jQuery: JavaScript / jQuery:

<script type="text/javascript">
    $(document).ready(function(){
        $("#event_trigger").fancybox({
            'padding'  : 0,
            'scrolling'  : 'no',
            'titleShow'  : false,
        });

        $("#events_form").bind("submit", function() {
            $.fancybox.showActivity();

            $.ajax({
                type  : "POST",
                cache : false,
                url  : "/events/index.php",
                data  : $(this).serializeArray(),
                success: function(data) {
                    $.fancybox(data);
                }
            });
            return false;
        });
    });
</script>

The PHP file returns and empty array. PHP文件返回并且为空数组。 However the Firebug post tab displays the form data. 但是, Firebug的“发布”选项卡显示表单数据。

Also, I noticed that if I do 另外,我注意到如果我这样做

print_r($_SERVER['REQUEST_METHOD'])

This returns GET, even though I have specified POST. 即使我指定了POST,它也会返回GET。

$(this).serializeArray()

with the name of the form CSS id (#my-form-ID, in this example) like this: 的格式为CSS id(在此示例中为#my-form-ID),如下所示:

$("#my-form-ID").serializeArray()

Hope that solves it. 希望能解决。 It worked for me. 它为我工作。 ;-D ;-D

$.ajax expects the parameter data to be an object or a string. $.ajax期望参数data是对象或字符串。

http://api.jquery.com/jQuery.ajax/ scroll down to data. http://api.jquery.com/jQuery.ajax/向下滚动到数据。

If you wrap your data in an object eg data: {array:$(this).serializeArray()} it may work. 如果将data包装在对象中,例如data: {array:$(this).serializeArray()}则可能有效。 I'm not 100% sure on that though. 不过,我不确定100%。

You are doing an AJAX request on a form submit. 您正在对表单提交进行AJAX请求。

Unless the AJAX request is synchronous (which I wouldn't recommend, anyway) there is a danger that your form will be submitted before there is any chance for the AJAX request will return. 除非AJAX请求是同步的(无论如何我还是不建议这样做),否则有可能在AJAX请求可能返回之前提交您的表单。

In the line: 在该行中:

$(this).serializeArray()

$(this) is referring to the the form element you have selected in the bind method. $(this)指的是您在bind方法中选择的form元素。 I'm assuming this is intended 我假设这是故意的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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