dongtanliefang8765 2017-07-25 00:36
浏览 124

如何使用POST请求将数据正确发送到PHP脚本?

UPDATE: Tried debugging using an absolutely wrong method. I was double-clicking the .php file through Network tabs and assumed that it was sending the same headers as it would through the ajax POST request. That's why GET was working for me (because the data is packaged within the URL, double clicking the php file meant I was simulating exactly what would happen in the ajax request).

Thanks to @Phil for pointing me in the right direction: After using error to print back the resulting data, I found that the responseText included what I originally wanted from the POST request.

Below this point is the original post

I am trying to send 2 pieces of information through a POST request without using a form.

It seems to be sending the data properly as ''Form Data'' when I look at the headers for the php file in Chrome's Network tab. However, using print_r($_POST) to check if the data is being retrieved on the PHP side results in just an empty array : "Array ( )"

JS

url = "/members/duc.php";
data = {
            "d": "test",
            "r": 156
        };
$.ajax({
            'type': "POST",
            'async': false,
            'global': false,
            'url': url,
            'data': data,
            'dataType': "json",
            'success': function (data) {
                jsonContainer = data;
                console.log('success');
            }
        });

PHP

print_r($_POST);

Attempts at debugging
5. Changing ajax method: 'POST' to method: 'GET' showed the data on the server side with the code print_r($_GET).
1. I have tried $_REQUEST and also setting an arbitrary var = file_get_contents("php://input"); 2. I have tried sending as json then decoding input.
3. I have tried using $.post instead of $.ajax.
4. I have tried setting the data in ajax to {duc:data}
6. Tried the POST method with www. included in the address bar, no change.

$_GET was working because



  • 写回答

1条回答 默认 最新

  • dpzlz08480 2017-07-25 04:41
    关注

    dataType:'json' is strict,check your php echo data type,make sure is json

    try this

    //js
    url = "/members/duc.php";
    data = {
                "d": "test",
                "r": 156
            };
    $.ajax({
                'type': "POST",
                'async': false,
                'global': false,
                'url': url,
                'data': data,
                //'dataType': "json",
                'success': function (data) {
                    jsonContainer = data;
                    console.log('success');
                }
            });
    //php
    print_r($_POST);
    

    or

    //js
    url = "/members/duc.php";
    data = {
                "d": "test",
                "r": 156
            };
    $.ajax({
                'type': "POST",
                'async': false,
                'global': false,
                'url': url,
                'data': data,
                'dataType': "json",
                'success': function (data) {
                    jsonContainer = data;
                    console.log('success');
                }
            });
    //php
    echo json_encode($_POST);
    

    will get the data, but I think second is better.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line