简体   繁体   English

Ajax POST方法在PHP中不起作用,但是GET在起作用

[英]Ajax POST method not working in PHP, but GET is working

I am trying to send using Ajax a POST request to php. 我正在尝试使用Ajax向php发送POST请求。

If I use GET it works fine, but with POST the data I receive in php is empty. 如果我使用GET可以正常工作,但是使用POST时,我在php中收到的数据为空。 I'm sending data as a json. 我正在将数据作为json发送。

This is what the js code looks like: js代码如下所示:

$.ajax(
        {
            type: 'POST',
            url: 'php/GiveItBack.php',
            contentType: "json",
            data: {
                word: 'abc'
            },
            success: function (json) {
                alert(json);
            },
            error: function (jqXHR, textStatus, errorThrown) { alert('ERROR: ' + errorThrown); }
        });

This is the php/GiveItBack.php file 这是php / GiveItBack.php文件

<?php

$x = $_GET['word'];
echo 'Get: ' . $x;

$x = $_POST['word'];
echo '; Post: ' . $x;

$x = $_REQUEST['word'];
echo '; Request: ' . $x . ';';

?>

With this code, the message in the alert window looks like this: 使用此代码,警报窗口中的消息如下所示:

Get: ; 获取: Post: ; 帖子:; Request: ; 要求:

If I replace type: 'POST' in the js file with type: 'GET' this is the result I get in the alert window (as I was expecting to see): 如果我将js文件中的类型:“ POST”替换为类型:“ GET”,这是我在警报窗口中得到的结果(正如我期望的那样):

Get: abc; 得到:abc; Post: ; 帖子:; Request: abc; 要求:abc;

I can't see what I'm missing here. 我看不到我在这里想念的东西。 Is something wrong in the code or is any special setting I need to do for this to work. 代码中有什么问题吗,或者我需要为此进行任何特殊设置。

By the way I am using: jquery-2.2.4.min and php v5.6 and XAMPP v3.2.2. 顺便说一下,我正在使用: jquery-2.2.4.min和php v5.6和XAMPP v3.2.2。

Thank you. 谢谢。

The content type was not correct, need to use contentType: "application/x-www-form-urlencoded" OR 'Content-Type': 'application/json' 内容类型不正确,需要使用contentType: "application/x-www-form-urlencoded"'Content-Type': 'application/json'

   $.ajax(
    {
        type: 'POST',
        url: 'php/GiveItBack.php',
        contentType: "application/x-www-form-urlencoded",
        data: {
            word: 'abc'
        },
        success: function (json) {
            alert(json);
        },
        error: function (jqXHR, textStatus, errorThrown) { alert('ERROR: ' + errorThrown); }
    });

contentType: "json",

You content type is wrong here. 您的内容类型在这里是错误的。 If you want to receive it the way you are trying, you should use application/x-www-form-urlencoded . 如果您想以尝试的方式接收它,则应该使用application/x-www-form-urlencoded

If you still want to stick within JSON, you will have to json_decode your PHP input: 如果您仍然想使用JSON,则必须对JSON输入进行json_decode

$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE);
$.ajax(
    {
        method: 'POST',
        url: 'php/GiveItBack.php',
        data: {
            word: 'abc'
        },
        success: function (json) {
            alert(json);
        },
        error: function (jqXHR, textStatus, errorThrown) { alert('ERROR: ' + errorThrown); }
    });

Remove contentType: json and you should be able to use the $_POST superblogal array. 删除contentType: json ,您应该可以使用$_POST superblogal数组。 If you use contentType: json the object containing the parameters is converted into a string and sent over to the server. 如果使用contentType: json则包含参数的对象将转换为字符串,然后发送到服务器。 To get the string you will need to use 要获取字符串,您将需要使用

file_get_contents('php://input');

This happens because $_POST only contains data sent along with the following headers: 发生这种情况是因为$_POST仅包含与以下标头一起发送的数据:

  • application/x-www-form-urlencoded 应用程序/ x-WWW窗体-urlencoded
  • multipart/form-data-encoded 多部分/格式的数据编码

When you set contentType: json , jQuery adds the application/json header to the request which is not supported by $_POST so the JSON string is treated as raw input and therefore you need to use the php://input stream to retrieve it 当您设置contentType: json ,jQuery向$_POST不支持的请求中添加了application/json头,因此JSON字符串被视为原始输入,因此您需要使用php://input流来检索它

Ajax Data Remove the contentType: json : Ajax数据删除contentType: json

$.ajax(
        {
            type: 'POST',
            url: 'php/GiveItBack.php',

        data: {
            word: 'abc'
        },
        success: function (json) {
            alert(json);
        },
        error: function (jqXHR, textStatus, errorThrown) { alert('ERROR: ' + errorThrown); }
    });

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

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