【问题标题】:Slim PHP returning JSONSlim PHP 返回 JSON
【发布时间】:2012-12-18 12:07:22
【问题描述】:

所以基本上我有我的主干应用程序对我的苗条 php 应用程序进行 ajax GET 调用。它期望 JSON dataType 作为回报。

$.ajax({
        url: './myroute',
        type: 'GET',
        dataType: "json",
        data: { username: username, password: password },
        success: function(data) {},
        error: function(data) {}
});

在我的苗条文件中,我有:

$app->get('/myroute', function() use ($app) {

    // all the good stuff here (getting the data from the db and all that)

    $dataArray = array('id' => $id, 'somethingElse' => $somethingElse);

    $response = $app->response();
    $response['Content-Type'] = 'application/json';
    $response->body(json_encode($dataArray));

});

    // Tried with these in the above GET request as well:
    // $app->contentType('application/json');
    // echo json_encode($dataArray);

虽然我的请求正确通过 (200),并且我正确获取了 JSON 数据,但错误是因为它还返回了完整的 index.php 页面数据(我的 javascript dataType: "json" 不允许,触发错误)

我认为将内容类型设置为“application/json”可以解决这个问题,但它仍然会返回完整的页面内容以及 json 数据。

编辑以供参考

我曾经设置它,以便 Slim 将我的 html 呈现为:

$app->get('/', function () use ($app) {
    // would just have my main page files in home.php instead of index.php
    $app-render('home.php');
});

这样,没有从 index.php 返回的 html 页面数据。但是 pushState 的方式是,我必须让我的 javascript 脚本在 index.php 上运行,否则我的页面将无法正确加载,因为当它们被请求时,脚本不在那里代表路由应该去哪里。

感谢任何帮助!

谢谢!

【问题讨论】:

  • 问题已解决:添加了 exit();在 $response->body(); 之后

标签: jquery ajax json backbone.js slim


【解决方案1】:

不熟悉 slim 框架。看起来很有趣。听起来代码在显示 json 后继续运行。也许在用你的 json 响应后尝试exit;ing php 应用程序?

$app->get('/myroute', function() use ($app) {
    // all the good stuff here (getting the data from the db and all that)

    $dataArray = array('id' => $id, 'somethingElse' => $somethingElse);

    $response = $app->response();
    $response['Content-Type'] = 'application/json';
    $response->body(json_encode($dataArray));
    exit();
});
$app->run();

希望对你有帮助

【讨论】:

  • 我没有放置 exit();在 $app-run() 下方的底部; .. 但是我确实把它直接放在了我的 $response->body(); 之后线,它就像一个魅力!谢谢楼主!
  • 如果你的答案反映了对我有用的东西,我会检查你!
  • 假设为 200。最好包含相应的 HTTP 响应代码以获取更多上下文。
  • exit() 来说不是一个好主意,路由回调必须返回一个响应对象。 Slim 有一些创建 JSON 响应的方法,但是如果你想写一些东西到响应体,你应该在最后返回响应对象。
  • @Nima 你可能是对的,返回 $response 可能是一个更好的解决方案。
【解决方案2】:

尝试将body() 更改为write()

我认为这会起作用:$response->write(json_encode($dataArray));

【讨论】:

    【解决方案3】:

    Slim documentation about returning JSON 提到了您正在寻找的内容。

    同样来自documentations,每个路由回调接受三个参数:

    请求
    第一个参数是代表当前 HTTP 请求的 Psr\Http\Message\ServerRequestInterface 对象。
    响应
    第二个参数是代表当前 HTTP 响应的 Psr\Http\Message\ResponseInterface 对象。
    参数
    第三个参数是一个关联数组,其中包含当前路由的命名占位符的值。

    引用同一页:

    如果你使用闭包实例作为路由回调,闭包的状态将绑定到容器实例。这意味着您将可以通过 $this 关键字访问闭包内的 DI 容器实例。

    所以在定义路由回调时不需要uese ($app)

    也如documentation中所述

    最终,每个 Slim 应用路由都必须返回一个 PSR 7 响应对象

    总而言之,这应该符合您的预期:

    $app->get('/myroute', function($request, $response, $args) {
    
    // all the good stuff here (getting the data from the db and all that)
    
    $dataArray = array('id' => $id, 'somethingElse' => $somethingElse);
    
    return $response->withJson($dataArray);
    
    });
    

    【讨论】:

      【解决方案4】:

      exit(); 不再为我工作,不知道为什么。以下是我的解决方法:

      $response = $app->response();
      $response['Content-Type'] = 'application/json';
      $response->body(json_encode($body));
      return $response;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        相关资源
        最近更新 更多