jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – Slim PHP返回JSON大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以基本上我有我的骨干应用程序对我的苗条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的方式是,我必须在index.PHP上运行我的javascript脚本,否则我的页面将无法正确加载,因为当它们被请求时,脚本不在那里委托路径应该去的地方.

任何帮助表示赞赏!

谢谢!

解决方法

不熟悉超薄框架.看起来很有趣听起来像json显示代码一直在运行.也许尝试退出;用你的json回复后的程序?
$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();

希望这可以帮助

大佬总结

以上是大佬教程为你收集整理的jquery – Slim PHP返回JSON全部内容,希望文章能够帮你解决jquery – Slim PHP返回JSON所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: