weixin_33711647 2015-10-13 21:03 采纳率: 0%
浏览 27

没有成功的POST数据html

when i make a request with ajax i can see that the post value, but when i see the console log it return me this message ---

no success post data

enter image description here

and also in the symfony web debug toolbar i can see what there are no ajax request have been make .

enter image description here

This is my html code ---

<form method="post" id="searchform">
        <div align="center" class="col-md-10">
            <input  type="text" id= "contentSearch" name="contentSearch" >
        </div>
    <div class="form-group"><button type="submit" class="btn btn-default" id="submitSearch">
            Search
        </button></div>
</form>

This is my javascript code ---

    <script>
$(document).ready(function () {
$("#searchform").on('submit', function (e) {
    $.ajax({
        url: '/home',
        type: 'post',
        data: { contentSearch : $('#contentSearch').val()},
        success: function (data) {
            console.log(data)
        }
    });

   return false;
});
});
    </script>

Controller code ---

routing code ---

content:
    path:     /home
    defaults: { _controller: AdminBundle:Home:home }

Controller code ---

<?php
namespace AdminBundle\Controller;    
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;    
class homeController extends Controller {
/**
 * @param Request $request
 * @return Response
 */
 // route of /home
function homeAction(Request $request) {

 if ($request->getMethod() == 'POST') {
 $request = $request->request->get('contentSearch');
 // do something with the  $request
 $result;
}}
return $this->render('AdminBundle:Home:home.html.twig', array(
        'pageTitle' => 'my ajax',
        'menu' => $this->menu,
        'result' => $result
            )
);

Do any one knows how to solve this problem !!!!

  • 写回答

2条回答 默认 最新

  • 衫裤跑路 2015-10-13 21:15
    关注

    when you submit a form, the default behaviour is to load the form "action" page with the supplied input fields. if no action is specified, it simply passes the value to the current page and reloads it.

    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3

    What you need to do is prevent the form from submitting so the ajax request can fire. use preventDefault to achieve this.

    $("#searchform").on('submit', function (e) {
    e.preventDefault();
    $.ajax({
        url: '/home',
        type: 'post',
        data: { contentSearch : $('#contentSearch').val()},
        success: function (data) {
            console.log(data)
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。