dongsuoying9059 2016-04-02 03:49
浏览 26

如何在Silex插入帖子中使用jQuery ajax

I'm trying to do some insert into the database then view the post list and display it by ajax, but i'm not pretty sure how to do it.

Here's my route:

$app->match('/status', 'Post\StatusPost\Controller\StatusPostController::statusAction')->bind('main');

Then on my controller I process the insert stuffs then access the Model entity and pass it into the view

$posts = new Post();
$posts = $posts->allPosts($app);

return $app['twig']->render('status.html.twig', [
  'form' => $form->createView(),
  'posts' => $app->json($posts),
]);

Then on my view there's just a simple text area. So upon submitting the post it should display it via ajax

Here's my js script

$('#status-btn').click(function(){
$.getJSON('/status', function(data){
  console.log(data);
  var html;
  $.each(data, function(entryIndex, entry){
    html += '<div class="status-container">';
    html += '<h3 class="status-title">' + entry.name + '</h3>';
    html += '<img src="' + window.location.href + '/images/' + entry.image + '">';
    html += '<p class="status-content">' + entry.status + '</p>';
    html += '<span class="status-time">' + entry.timestamp + '</p>';
    html += '</div>';
  });
  $('.content-container').html(html);
}); 

});

But upon submitting it nothing happens.

  • 写回答

1条回答 默认 最新

  • dstbp22002 2016-04-03 07:32
    关注

    It looks like that you say GetJSON, but you send Html? cannot work. you should send a JsonResponse or you must set the Headers properly 'Content-Type: application/json'. If you use GetJSON the browser tries automatically convert the response from the php script (no matter if its silex) and the header must be of type JSON.

    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀