使用jquery.ajax()类型发布错误的帖子数据,并使用php和.htaccess

时间:2016-03-07 16:08:29

标签: php jquery json ajax .htaccess

我正在使用JSONAP规范开发api来处理PHP的多个insert语句。我一直在引用这个链接来创建我的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来清理网址。 .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查询字符串,我从中得不到任何东西。知道我在这里做错了吗?我能够使用相同的方法成功获取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,request,server [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

1 个答案:

答案 0 :(得分:1)

当您使用contentType:'application/json'发送数据时,$_POST将为空。

要访问它,请尝试:

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