简体   繁体   English

从ajax请求php获取发布数据

[英]Getting post data from ajax request php

I keep getting null values with the below code:我使用以下代码不断获取空值:

Ajax request: Ajax 请求:

formData = {
            u: "3959eeadb32e02b85a792e21c",
            id: "6d7613df26"
        };
        $.ajax({
            url: "/includes/loginProcess.php",
            type: "POST",
            crossDomain: true,
            contentType: 'application/json',
            data: formData,
            dataType: "json",
            success: function(data) {
                console.log(data);
            },
            error: function() {
                
            }
        });

PHP Processing: PHP处理:

$data = array();
    $data['u'] = $_POST["u"];
    
    $data['id'] = $_POST["id"];
    echo json_encode($data); 

{u: null, id: null} is logged in the success function. {u: null, id: null} 登录成功函数。 Any ideas?有任何想法吗?

This is on an AWS Lightsail instance BTW... is there some server config I'm missing maybe?这是在 AWS Lightsail 实例上 顺便说一句...是否有一些我遗漏的服务器配置?

contentType: 'application/json'

is wrong.是错的。 You aren't sending JSON.您没有发送 JSON。 This will confuse the server.这会混淆服务器。 Just remove that line and it will work fine.只需删除该行,它就会正常工作。

(Also crossDomain: true almost certainly isn't needed here (and probably doesn't do whatever you think it does)). (还有crossDomain: true在这里几乎肯定不需要(并且可能不会做您认为的任何事情))。

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

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