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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!