简体   繁体   English

PHP和jQuery AJAX-$ _POST始终为空

[英]PHP and jQuery AJAX - $_POST is always empty

I am trying to output my $_POST value in the alert box with jQuery AJAX function, but the $_POST value from my PHP controller is always empty and I am always getting an empty array in the response. 我正在尝试使用jQuery AJAX函数在警报框中输出$ _POST值,但是PHP控制器中的$ _POST值始终为空,并且响应中始终为空数组。

I am using PHP version 5.5, jQuery version 2.2.3 我正在使用PHP 5.5版,jQuery 2.2.3版

(I have tried multiple variants of possible solutions for this issue, but still I can't find a correct way to fix this). (我已经针对此问题尝试了多种可能的解决方案,但仍然找不到解决此问题的正确方法)。

1. This is my AJAX code (I didn't made it with submit function, I am just updating some content on my web page without redirection): 1.这是我的AJAX代码(我没有使用Submit函数实现它,我只是在不重定向的情况下更新网页上的某些内容):

$.ajax({
                method: 'POST',
                url: '../OrganizationController.php',
                dataType: "json",
                contentType: "application/json",
                data: {'myForm':$('#myForm').serialize()},
                success: function(data){
                    alert(data);
                },
                error: function(xhr, desc, err){
                    console.log(err);
                }
            });

2. There is my simple PHP controller: 2.有一个简单的PHP控制器:

<?php

include('../mysql_conn.php');

$value;
if ($_POST['myForm']) {
    $value= $_POST['myForm'];
    header('HTTP/1.1 200 OK');
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

header('Content-Type: application/json');
echo json_encode($value);
...
?>

3. My simple form: 3.我的简单表格:

 <form id="myForm">
    <input type="text" name="name" title="Name"/>
    <input type="button" name="click">Press</input>
</form>

Controller is working. 控制器正在工作。 If I want to output a response with my input or some test text - everything is working and I get text in the success alert box. 如果我想用输入或一些测试文本输出响应,则一切正常,并且在成功警报框中显示文本。

I have tried different ways and saw almost all answers for similar issues. 我尝试了不同的方法,并看到了类似问题的几乎所有答案。

I have tried to deleted dataType , tried to change it to text , tried to remove contentType , added an object to data like in many answers people advised, tested it with simple values like {'name':John} etc., tried to make a very simple form with one input and text-field. 我尝试删除dataType ,尝试将其更改为text ,尝试删除contentType ,向数据中添加对象,就像人们建议的许多答案一样,并使用{'name':John}等简单值进行了测试,试图使一种具有一个输入和文本字段的非常简单的形式。 Nothing helped. 没有任何帮助。

Question: 题:

Can you advise for this issue or some small example with my code or with other similar code with possible solution for my problem? 您能用我的代码或其他类似代码为这个问题或一些小示例提供建议,并为我的问题提供可能的解决方案吗?

Edit: 编辑:

We have a very informative discussion , but unfortunately none of this solutions helped. 我们进行了非常有益的讨论 ,但不幸的是,这些解决方案都没有帮助。 But now we understood that in the other browsers or computes everything is working fine and the problem may be more serious (in configurations etc.). 但是现在我们了解到,在其他浏览器或计算中,一切正常,问题可能更加严重(在配置等方面)。

In your PHP file, you can only retrieve the input parameters by their name. 在您的PHP文件中,您只能按名称检索输入参数。 Like, in your form, if you have email or phone number and you are sending them to your controller file then get it by their name in your controller file instead of form name. 就像在您的表单中一样,如果您有电子邮件或电话号码,并且正在将它们发送到控制器文件,则可以通过其在控制器文件中的名称(而不是表单名称)来获取它们。

Here is the correct code to do this: 这是执行此操作的正确代码:

$.ajax({
     method: 'POST',
     url: 'test.php',
     dataType: "json",
     data: $('#myForm').serialize(),
     success: function(data) {
         alert(data);
     },
     error: function(xhr, desc, err) {
         console.log(err);
     }
 });

And in your PHP file, get the values like 然后在您的PHP文件中,获取如下值

<?php
if ($_POST['name']) {
    $value= $_POST['name'];
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

header('Content-Type: application/json');
echo json_encode($value);
?>

Here I assume that email is a field in your form. 在这里,我假设电子邮件是您表单中的一个字段。

Try this! 尝试这个!

Edit: Just replaced POST param in PHP file because I saw that "name" is one of the fields in your form. 编辑:刚刚替换了PHP文件中的POST参数,因为我看到“名称”是您表单中的字段之一。 Cheers! 干杯!

PS: contentType: application/json doesn't work sometimes. PS:contentType:application / json有时不起作用。 Here you can see some threads on it: Github Thread 在这里您可以看到一些线程: Github线程

The problem is because you're placing form-encoded data within a JSON object. 问题是因为您要将表单编码的数据放在JSON对象中。 Aside from being redundant, all this will do is confuse your PHP code. 除了冗余之外,所有要做的就是混淆您的PHP代码。

To fix the problem, just send the serialised form and use $_POST to retrieve the sent fields as normal. 要解决此问题,只需发送序列化的表单并使用$_POST照常检索发送的字段。

$.ajax({
    method: 'POST',
    url: '../OrganizationController.php',
    dataType: "json",
    data: $('#myForm').serialize(),
    success: function(data){
        alert(data);
    },
    error: function(xhr, desc, err){
        console.log(err);
    }
});
if ($_POST['input-field-name-here']) {
    // do something with the data here...

    header('HTTP/1.1 200 OK');
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

I finally found the solution! 我终于找到了解决方案!

Three days I was trying to fix this issue. 我三天试图解决此问题。 I was using PHPStorm IDE v.2016.1 with it's own Build-in server. 我将PHPStorm IDE v.2016.1与它自己的内置服务器一起使用。

So when I have switched to the XAMPP with it's own Apache server - the problem was solved. 因此,当我使用自己的Apache服务器切换到XAMPP时,问题就解决了。 All is working fine in different variants, so the problem was not in my code. 一切都可以在不同的版本中正常工作,因此问题不在我的代码中。 I found that this is the real bug in PHPStorm 我发现这是PHPStorm中的真正错误

To solve this issue: Just switch to another server and don't use PHPStorm ps even when I have changed multiple servers in my PHPStorm IDE - problem wasn't solved, so be carefull. 解决此问题的方法:即使我在PHPStorm IDE中更改了多台服务器,也只需切换到另一台服务器就不要使用PHPStorm ps-问题尚未解决,因此请小心。

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

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