【问题标题】:Phalcon getJsonRawBody not working with GET requestPhalcon getJsonRawBody 不适用于 GET 请求
【发布时间】:2014-07-12 05:50:10
【问题描述】:

我正在使用 Phalcon 开发一个 API。我正处于试错阶段,正在学习 Phalcon 和使用此框架构建 REST API 的正确方法。

我的问题是,当通过 jQuery 对我的 API 进行 ajax 调用时,类型为:“GET”Phalcon 无法使用 getJsonRawBody 解析请求。当我通过 jQuery 使用 type: "POST" 运行相同的 ajax 调用时,它可以工作。


这是我的 ajax 请求。

$('.start').click(function() {
    var arr = {timestamp:'<?=time();?>'};
    $.ajax({
        type: "GET",
        beforeSend: function (request)
        {
            request.setRequestHeader("X-Api-Key", "<?=$api_key;?>");
            request.setRequestHeader("X-Hash", "<?=$hash;?>");
        },
        data: JSON.stringify(arr),
        contentType: 'application/json; charset=utf-8',
        url: "http://api-dev.fancompetition.com/my-rest-api/api/robots/",
    })
    .done(function(data) {
        $('#data').html(data);
    });
});

在服务器端我有:

$vars = $app->request->getJsonRawBody();

所以现在如果您将类型更改为 POST,我可以获取内容。当类型为 GET 时,我的变量 vars 为空白。知道为什么吗?

【问题讨论】:

  • 得到了答案。基本上 GET 请求通常没有消息体。在 url 中作为参数传递并在服务器端正确捕获。

标签: jquery ajax phalcon


【解决方案1】:

GET 请求没有正文。如果需要传递数据,可以添加查询字符串。例如:

http://api-dev.fancompetition.com/my-rest-api/api/robots/?key1=value1&key2=value2

您可以通过$app-&gt;request-&gt;get()检索,然后使用json_decode手动解码json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2019-04-11
    • 2020-07-19
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多