>我正在使用JSONAP规范开发一个api来处理PHP的多个插入语句。 我一直在引用此链接来创建我的 JSON。 http://jsonapi.org/extensions/bulk/#creating-multiple-resources
我使用 jquery 提交我的表单,并运行此代码
$.ajaxSetup({
cache:false
,contentType:"application/vnd.api+json; ext=bulk"
,accept:"application/vnd.api+json; ext=bulk"
,dataType:"json"
});
var jsonObject = buildJsonInsert();
//console.log(jsonObject);
$('#remarks').append(JSON.stringify(jsonObject));
$.ajax({
type: "POST"
,url: "/api/STYLE3/"
,data: JSON.stringify(jsonObject)
,success: function (data) {
console.log(data);
}
,error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
console.log(ajaxOptions);
}
,complete: function () {
console.log('complete');
}
});
我正在使用的 JSON 是有效的,因为我已经用 http://www.jsonlint.com 测试了它 正在传递的 JSON 是这个
{"data":[{"type":"inventory","attributes":{"stock":"4512","date":"2016-02-01","opc":"tb","wd":"E","sze":"030","qty":"1","rem":"TEST","type":"A"}},{"type":"inventory","attributes":{"stock":"4512","date":"2016-02-01","opc":"tb","wd":"E","sze":"035","qty":"2","rem":"TEST","type":"A"}},{"type":"inventory","attributes":{"stock":"4512","date":"2016-02-01","opc":"tb","wd":"E","sze":"040","qty":"3","rem":"TEST","type":"A"}},{"type":"inventory","attributes":{"stock":"4512","date":"2016-02-01","opc":"tb","wd":"E","sze":"045","qty":"4","rem":"TEST","type":"A"}}]}
这被发送到我的 api.php 文件,该文件首先通过我的 .htaccess 来清理 url。 .htaccess看起来像这样。
RewriteEngine On
RewriteBase /
Options +FollowSymlinks -Indexes -MultiViews
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-zA-Z0-9]+)/ api.php [L]
在我的 api .php文件中,我从 $_SERVER 变量中检查适当的请求方法,然后处理发布的数据。 请求方法显示为 post,但不显示任何 post 变量。 所以我检查了获取、请求和服务器查询字符串,但我从他们那里没有得到任何东西。 知道我在这里做错了什么吗? 我能够使用相同的方法成功执行GET请求来检索数据。 这里只是 POST 数据丢失。
原料药.php:
echo 'SERVER:';
print_r($_SERVER);
echo '<br />';
echo 'REQUEST:';
print_r($_REQUEST);
echo '<br />';
echo 'POST:';
print_r($_POST);
echo '<br />';
echo 'GET:';
print_r($_GET);
echo '<br />';
结果:
SERVER:Array
(
[CONTENT_LENGTH] => 578
[CONTENT_TYPE] => application/vnd.api+json; ext=bulk
[HTTP_ACCEPT] => application/json, text/javascript, */*; q=0.01
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
[HTTP_X_REQUESTED_WITH] => XMLHttpRequest
[QUERY_STRING] =>
[REDIRECT_STATUS] => 200
[REDIRECT_UNIQUE_ID] => Vt2gbhdbRg0AABIqAhgAAAAb
[REDIRECT_URL] => /api/STYLE3/
[REQUEST_METHOD] => POST
[REQUEST_URI] => /api/STYLE3/
[SCRIPT_NAME] => /api.php
[SERVER_PORT] => 80
[UNIQUE_ID] => Vt2gbhdbRg0AABIqAhgAAAAb
[PHP_SELF] => /api.php
[REQUEST_TIME_FLOAT] => 1457365102.7973
[REQUEST_TIME] => 1457365102
[argv] => Array
(
)
[argc] => 0
)
REQUEST:Array
(
[__unam] => 7639673-14e20c34333-549bc39e-14
[PHPSESSID] => 9decjei6e70nreuu7028cdojq4
[_ga] => GA1.2.1543525819.1443120533
[_asomcnc] => 1
)
POST:Array
(
)
GET:Array
(
)
尝试数据:jsonObject 导致对象被发送为这样,但在 get、post、request、服务器[query_string] 中仍然没有任何内容
data%5B0%5D%5Btype%5D=inventory&data%5B0%5D%5Battributes%5D%5Bstock%5D=4512&data%5B0%5D%5Battributes%5D%5Bdate%5D=2016-02-01&data%5B0%5D%5Battributes%5D%5Bopc%5D=tb&data%5B0%5D%5Battributes%5D%5Bwd%5D=E&data%5B0%5D%5Battributes%5D%5Bsze%5D=030&data%5B0%5D%5Battributes%5D%5Bqty%5D=1&data%5B0%5D%5Battributes%5D%5Brem%5D=JOSE+TEST&data%5B0%5D%5Battributes%5D%5Btype%5D=A&data%5B1%5D%5Btype%5D=inventory&data%5B1%5D%5Battributes%5D%5Bstock%5D=4512&data%5B1%5D%5Battributes%5D%5Bdate%5D=2016-02-01&data%5B1%5D%5Battributes%5D%5Bopc%5D=tb&data%5B1%5D%5Battributes%5D%5Bwd%5D=E&data%5B1%5D%5Battributes%5D%5Bsze%5D=035&data%5B1%5D%5Battributes%5D%5Bqty%5D=2&data%5B1%5D%5Battributes%5D%5Brem%5D=JOSE+TEST&data%5B1%5D%5Battributes%5D%5Btype%5D=A&data%5B2%5D%5Btype%5D=inventory&data%5B2%5D%5Battributes%5D%5Bstock%5D=4512&data%5B2%5D%5Battributes%5D%5Bdate%5D=2016-02-01&data%5B2%5D%5Battributes%5D%5Bopc%5D=tb&data%5B2%5D%5Battributes%5D%5Bwd%5D=E&data%5B2%5D%5Battributes%5D%5Bsze%5D=040&data%5B2%5D%5Battributes%5D%5Bqty%5D=3&data%5B2%5D%5Battributes%5D%5Brem%5D=JOSE+TEST&data%5B2%5D%5Battributes%5D%5Btype%5D=A&data%5B3%5D%5Btype%5D=inventory&data%5B3%5D%5Battributes%5D%5Bstock%5D=4512&data%5B3%5D%5Battributes%5D%5Bdate%5D=2016-02-01&data%5B3%5D%5Battributes%5D%5Bopc%5D=tb&data%5B3%5D%5Battributes%5D%5Bwd%5D=E&data%5B3%5D%5Battributes%5D%5Bsze%5D=045&data%5B3%5D%5Battributes%5D%5Bqty%5D=4&data%5B3%5D%5Battributes%5D%5Brem%5D=JOSE+TEST&data%5B3%5D%5Battributes%5D%5Btype%5D=A
当您使用 contentType:'application/json'
发送数据时,$_POST
将为空。
要访问它,请尝试:
$data = json_decode(file_get_contents('php://input'));
print_r($data);