将请求发布到php脚本

时间:2019-03-06 21:12:21

标签: php node.js amazon-web-services post

在用户断开与节点服务器的连接后,我正在尝试向php文件发出发布请求。

节点服务器已通过SSL认证,并且托管php脚本。

function updateRank(user, rank) {
  var form = {
    user: user,
    rank: rank
  };

  var formData = querystring.stringify(form);
  var contentLength = formData.length;

  request({
    headers: {
      'Content-Length': contentLength,
      'Content-Type': 'application/x-www-form-urlencoded',
      'accept': '*/*',
      'user-agent':'*'
    },
    uri: 'https://wtf.com/pechp/updateUser.php',
    body: formData,
    method: 'POST'
  }, function (err, res, body) {
    //it works!
  });
}

发出请求时,我始终收到500错误。问题不在于代码。它与标题有关。我不知道怎么办。

这是我在本地运行时得到的响应:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)
localhost/:1 Failed to load https://wtf.com/pechp/updateUser.php: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

这是我的php文件:

    <?php

    ini_set('display_errors', 'On');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: X-Requested-With');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');

    // Create connection
    $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($connection) {
        $username = mysqli_real_escape_string($connection, $_POST['user']);
        $newRank = mysqli_real_escape_string($connection, $_POST['rank']);
        $result = mysqli_query($connection, "UPDATE wtf SET rank = '$newRank' WHERE username = '$username'");

        if 

($result) {
       echo "success"
    } else {
       echo "fail";
    }
} else {
    echo "connection failed";
}
exit;
?>

0 个答案:

没有答案