简体   繁体   English

PHP 没有看到来自 ajax 通过 serializeArray() 发布的数据

[英]PHP doesnt see posted data from ajax by serializeArray()

I have a form, running through jquery validation which then submits via ajax to a PHP script to handle backend functions.我有一个表单,通过 jquery 验证运行,然后通过 ajax 提交给 PHP 脚本以处理后端功能。 Ajax collects form values through serializeArray() and looks to do the job. Ajax 通过 serializeArray() 收集表单值,并希望完成这项工作。 Script fires and data is sent through(I think) to PHP.脚本触发并且数据通过(我认为)发送到 PHP。 I've tried probably close to 100 combinations to receive the data at the PHP side but with no luck.我已经尝试了可能接近 100 种组合来接收 PHP 端的数据,但没有运气。 I'm convinced this must be simple, something I've overlooked.我相信这一定很简单,我忽略了一些东西。 Code for the ajax is below, along with a screenshot of developer tools showing what's being sent.下面是 ajax 的代码,以及显示正在发送的开发人员工具的屏幕截图。

No matter what I try on the PHP side, I either get an empty array, NULL through $_POST/$_GET.无论我在 PHP 端尝试什么,我都会通过 $_POST/$_GET 得到一个空数组 NULL。 I've tried json_decode, parsing the string, var_dump etc.我试过 json_decode、解析字符串、var_dump 等。

var data=$(form).serializeArray();
$.ajax({
  cache: false,
  type: "POST",
  dataType: "JSON",
  url: "process/create_site.php",
  data: data,
  success: function(response) {
    console.log(response);
    //$(form).html("<div id='message'></div>");
    //$('#message').html("<h2>Your request is on the way!</h2>")
    //  .append("<p>someone</p>")
    //  .hide()
    //  .fadeIn(1500, function() {
    //    $('#message').append("<img id='checkmark' src='images/ok.png' />");
    //  });
  }
});

ajax 提交的 PHP 输出图像 正在提交的 ajax 的图像

I managed to get to the bottom of this, after an embarrassing amount of time.经过一段令人尴尬的时间后,我设法弄清楚了这一点。 I'd like to post the simple reason here to help others.我想在这里发布一个简单的原因来帮助别人。

The entire JS block was wrapped in $(document).ready(function(){ which was causing the values to be stripped when posting to the PHP.整个 JS 块被包装在$(document).ready(function(){中,这导致在发布到 PHP 时值被剥离。

I can't find any documentation or answer to a question with a similar scenario - so here it is!我找不到任何文档或对类似情况的问题的答案 - 所以就在这里!

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

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