Dev*_*vin 7 ajax jquery json slim backbone.js
所以基本上我有我的骨干应用程序对我的苗条php应用程序进行ajax GET调用.它期望JSON dataType作为回报.
$.ajax({
url: './myroute',
type: 'GET',
dataType: "json",
data: { username: username, password: password },
success: function(data) {},
error: function(data) {}
});
Run Code Online (Sandbox Code Playgroud)
在我的苗条文件中,我有:
$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);
Run Code Online (Sandbox Code Playgroud)
虽然我的请求正确地通过(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');
});
Run Code Online (Sandbox Code Playgroud)
就这样,没有从index.php返回的html页面数据.但是pushState的方式是,我必须在index.php上运行我的javascript脚本,否则我的页面将无法正确加载,因为当它们被请求时,脚本不在那里委托路径应该去哪里.
任何帮助表示赞赏!
谢谢!
vor*_*ent 10
不熟悉超薄框架.看起来很有趣 听起来像json显示后代码一直在运行.也许exit;在用你的json回复后试试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();
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助