【问题标题】:http post request not accepted by phpphp不接受http post请求
【发布时间】:2014-07-19 09:05:40
【问题描述】:

我是 php 和 stackovrflow 的新手。所以请原谅我最初的错误。 我正在通过 extjs4 应用程序发送发布请求。根据 Mozilla 控制台网络选项卡,发送请求。 PHP 是 5.4.25。详情如下:

POST /Opp/annt.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 83
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://localhost/opp/index.html?_dc=140
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Form Dataview parsed
aaa=i&bbb=i&ccc=&ddd=&eee=4&fff=5&ggg=&hhh=

PHP 文件:

header('Content-Type: application/json');
$method = $_SERVER["REQUEST_METHOD"];
$con = new PDO("mysql:host=localhost;dbname=openclass", "root", "")  or die("cannot connect to mysql");

    $fileContents = file_get_contents("php://input");
  echo $fileContents;
  json_decode($fileContents, true);

function getPostValue($postData, $fieldName) {
    return (!empty($postData[$fieldName]) ? htmlspecialchars($postData[$fieldName]) : NULL);
}

执行发布请求后,我没有收到任何数据。你能帮忙指出可能的问题和解决方案吗

【问题讨论】:

  • POST请求中的数据不是从输入中清除并放入$_POST$_FILES数组吗?
  • @user3855498 你想做什么? php的几乎每一行都是错的,什么都不做。
  • @EWit 不,不是。它被复制到$_POST,但您可以使用php://input 读取原始数据。
  • return 只能在函数内部使用。 return json_encode($fileContents, true) 在函数之外毫无意义。它应该echo json_encode($fileContents, true)。在此之前你不应该回显$fileContents——如果一个脚本应该返回JSON,那么它唯一应该回显的东西。
  • @Loz 我试图通过插入语句在 mysql 中插入数据。一切正常。我对发送 POST 请求的方式进行了一些更改,事情发生了变化。

标签: php post json


【解决方案1】:

我解决了这个问题。我收到错误,因为 http post 在 application/x-www-form-urlencoded 中发送表单数据; charset=UTF-8 格式。 我使用 AJAX 请求更改了发送到 json 的格式。这改变了发送到 json 键值对的格式。 这有助于它被 php 读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多