所以基本上我有我的骨干应用程序对我的苗条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();
希望这可以帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。