2016-03-07 21 views
0

我使用JSONAP规范开发一个API来处理PHP的多个插入语句。我一直在引用这个链接来创建我的JSON。 http://jsonapi.org/extensions/bulk/#creating-multiple-resources丢失发布数据与jquery.ajax()类型的职位与PHP和.htaccess到位

我使用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来清理网址。 .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,request和server query字符串,而且我没有从他们那里得到什么。任何想法我在这里做错了吗?我能够使用相同的方法成功执行GET请求来检索数据。只是POST数据丢失在这里。

api.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,请求,服务器[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 
+0

能不能请你'数据:jsonObject',而不是',数据:JSON.stringify(JSONObject的)'?在你的情况下,你发送一个字符串(编码的json)到你的服务器没有名字。 –

+0

试过了,我在请求底部发布了请求的结果。仍然在服务器端没有。 –

回答

1

当你与contentType:'application/json'然后$_POST无线发送数据将是空的。

要访问尝试:

$data = json_decode(file_get_contents('php://input')); 
print_r($data); 
+0

谢谢你做到了。 –