没有成功的POST数据html [英] no success POST data html
问题描述
当我使用ajax发出请求时,我可以看到该发布值,但是当我看到控制台日志时,它会向我返回此消息---
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
以及在symfony Web调试工具栏中,我可以看到没有发出任何Ajax请求.
and also in the symfony web debug toolbar i can see what there are no ajax request have been make .
这是我的html代码---
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>
这是我的JavaScript代码---
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>
控制器代码---
路由代码---
content:
path: /home
defaults: { _controller: AdminBundle:Home:home }
控制器代码---
<?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 !!!!
推荐答案
您要发送两次表单.如果使用$(form).on('submit')设置函数,则在发送表单之前,它将执行您告诉它的任何操作.您必须添加event.preventDefault()
You are sending the form twice. If you set a function with $(form).on('submit') then BEFORE the form is sent, it will do whatever you tell it to. You have to add event.preventDefault()
$(form).on('submit',function(event){
event.preventDefault()
// Do Ajax
})
并确保返回带有回声的Ajax响应.
And be sure to return your Ajax response with echo.
这篇关于没有成功的POST数据html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!