控制器返回纯json数据,而不是停留在页面上
Controller returns plain json data instead of staying on page
我正在尝试向我们发送一个返回搜索结果的ajax请求。当我提交搜索时,它会在一个纯黑白页面上返回我的json数据。我如何让它留在页面上并执行我的javascript的其余部分?我认为问题是event.preventDefault
工作不正常,或者控制器中的return
语句阻止了任何进一步的事情发生。
以下是我的代码片段:
HTML
<div id="contactform">
<p>Search for a Contact</p>
{{ Form::open(array('route' => 'contacts.results', 'method' => 'post', 'id' => 'contactsearchform')) }}
{{ Form::text('contactsearch', null, array('id' => 'contactsearch', 'placeholder' => 'Name')) }}
<button type="submit">Search</button>
{{ Form::close() }}
</div>
<div id="search_results">
Search Results
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<td class="name first">Name</td>
<td class="phone">Phone</td>
<td class="email">Email</td>
<td class="lot">Lot</td>
<td class="edit last"></td>
</tr>
</thead>
<tbody>
<tr>
<td class="name first"><a href="#" class="contact"></a></td>
<td class="phone"></td>
<td class="email"></td>
<td class="edit last"></td>
</tr>
</tbody>
</table>
</div>
Javascript
$('contactsearchform').submit(function(event) {
event.preventDefault();
var dataString = $(this).serialize();
console.log('Submitted Data:'n' + dataString);
$.ajax({
type: "POST",
url: "/contacts.results",
data: dataString,
error: function(){
console.log("Something is wrong. Please inform Admin");
},
success: function(resultData){
console.log('Result Data:'n' + resultData);
resultDataP = $.parseJSON(resultData);
console.log('Parsed Data:'n' + resultDataP);
if(resultDataP.table){
$('#search_results').html(resultDataP.table);
$('#search_results').fadeIn();
$('#contactsearchform input').blur();
}else{
console.log("Something didn't work.");
}
}
});
return false;
});
控制器
public function showSearch()
{
return View::make('portal.search');
}
public function doSearch()
{
// Search for contacts here
$search = Input::get('contactsearch');
$contacts = DB::table('contacts')->where('last_name', 'LIKE', "$search")->get();
if (count($contacts) != 0) {
$response = [
'status' => 'success',
'msg' => 'Contacts matched your search.',
'results' => $contacts
];
}
else {
$response = [
'status' => 'error',
'msg' => 'No contacts matched your search.'
];
}
return Response::json($response);
}
和我的路线
// Search for contacts
Route::get( '/portal/contacts', array(
'as' => 'contacts.search',
'uses' => 'PortalController@showSearch'
) );
Route::post( '/portal/contacts', array(
'as' => 'contacts.results',
'uses' => 'PortalController@doSearch'
) );
您应该稍微更改一下您的javascript,像使用一样使用它
/** consider the hash before the id of form you haven't added one so its not considering the form
also you need to change the url of you $.ajax call put url:'/portal/contacts' because jquery don't understand name of laravel route
**/
$('#contactsearchform').submit(function(e){
........
});
相关文章:
- _.map在返回时覆盖值
- Firefox,如何提交表单触发文件下载,但停留在网页上
- Jssor滑块停留在第一个图像上,直到我单击它
- 如何在返回单个对象时递归使用 Array.prototype.find()
- 在返回的 JSON 数据上创建 href
- $.getJSON 在返回数据后不会运行函数
- loadFeeds在返回页面时不会发出刺耳的声音
- 在浏览器关闭时,单击确认对话框中的“停留在页面上”,执行一个方法
- 在返回私有函数之前发生作用域泄漏
- 可以'在html中,没有任何东西可以停留在图片上方
- 如何在D3中添加一个强制拖动事件,并使节点停留在我离开它的地方
- ASP.net自定义控件,下拉值必须在服务器端填充,所选值在返回时丢失
- javascript onmouseover/omouseout停留在前一个内容中,直到悬停/触发下一个内容
- 如果用户由于第二个处理程序中的对话框而停留在页面上,则跳过加载前处理程序
- 当结果点击值停留在文本框中时,自动完成搜索
- 函数返回undefine,即使我在返回之前检查了变量是否未定义
- ASP WebForms在返回后重新评估属性
- 如何在返回另一个函数中的值后运行一个函数
- 学习Javascript,但停留在一个简单的返回关键字上
- 控制器返回纯json数据,而不是停留在页面上