Javascript 使用AJAX的laravel5jquery
我使用的是Laravel5,我在控制器的一些集合上做了一些过滤。问题在于使用AJAX作为刀片模板和控制器之间的桥梁。以下是我的jQuery代码:Javascript 使用AJAX的laravel5jquery,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我使用的是Laravel5,我在控制器的一些集合上做了一些过滤。问题在于使用AJAX作为刀片模板和控制器之间的桥梁。以下是我的jQuery代码: function listCreatedBy(str) { $.ajax({ headers : { 'csrftoken' : '{{ csrf_token() }}' }, url: '{{ url("search") }}',
function listCreatedBy(str) {
$.ajax({
headers : {
'csrftoken' : '{{ csrf_token() }}'
},
url: '{{ url("search") }}',
type: "get", //send it through get method
data:{txt:str},
success: function(response) {
console.log("ola");
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
这是我的路线:
Route::get('/search/{txt}', 'PrestashopController@search')->name('search');
这是我在控制器中的方法:
public function search($searchText){
var_dump($searchText);
return "results"; //Just to see if it's returning correctly
}
public function getSearch(Request $request){
dd($request->txt);
return "results"; //Just to see if it's returning correctly
}
这个方法现在是空的,因为我只想看看是否可以先完成ajax代码。但它返回的是404代码,找不到。知道我做错了什么吗?试着这样做
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
试着这样做
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
试试这个:
$.ajax({
url: '{{ url("/route_name") }}',
// here route_name is a named route that call a controller function
type: "get", //send it through get method
data:{txt:str},
success: function(response) {
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
试试这个:
$.ajax({
url: '{{ url("/route_name") }}',
// here route_name is a named route that call a controller function
type: "get", //send it through get method
data:{txt:str},
success: function(response) {
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
请尝试使用完整路径URL
url: '{{ url("/search") }}',
让你的AJAX像这样
$.ajax({
url: '{{ url("/search") }}',
type: "get", //send it through get method
data:{txt:str},
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
请尝试使用完整路径URL
url: '{{ url("/search") }}',
让你的AJAX像这样
$.ajax({
url: '{{ url("/search") }}',
type: "get", //send it through get method
data:{txt:str},
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
我想你有打字错误
Route::get('/search/{$txt}'
不带$
还有一个..尝试在控制器方法中在作用域中使用此代码,而不是$searchtext
public function search(Request $request)
然后访问$txt变量,如下所示
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
)
在jQuery代码中使用以下命令:
function listCreatedBy(str) {
var query_url = '{{ url("search") }}' + str;
$.ajax({
url: query_url ,
type: "get", //send it through get method
success: function(response) {
console.log("ola");
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
注意:csrftoken用于提交表单我认为您有打字错误
Route::get('/search/{$txt}'
不带$
还有一个..尝试在控制器方法中在作用域中使用此代码,而不是$searchtext
public function search(Request $request)
然后访问$txt变量,如下所示
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
)
在jQuery代码中使用以下命令:
function listCreatedBy(str) {
var query_url = '{{ url("search") }}' + str;
$.ajax({
url: query_url ,
type: "get", //send it through get method
success: function(response) {
console.log("ola");
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
注意:csrftoken用于提交表单将其添加到ajax调用中
headers : {
'csrftoken' : '{{ csrf_token() }}'
}
编辑
我看见你在走这条路
Route::("/search/{txt}" ...
女巫对应
http://example.com/search/random%20text
可能发生的情况是你走错了路线
您正在进行的ajax调用将创建如下uri
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
试试这个
$.ajax({
headers : {
'csrftoken' : '{{ csrf_token() }}'
},
url: "{{ url("search") }}/" + encodeURIComponent(str),
type: "get", //send it through get method
success: function(response) {
console.log("ola");
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
将此添加到ajax调用中
headers : {
'csrftoken' : '{{ csrf_token() }}'
}
编辑
我看见你在走这条路
Route::("/search/{txt}" ...
女巫对应
http://example.com/search/random%20text
可能发生的情况是你走错了路线
您正在进行的ajax调用将创建如下uri
$.ajax({
url: "/search/"+str,
type: "GET",
data:'',
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
Route::get('/search/{txt}', 'PrestashopController@getSearch');
public function getSearch($searchText){
dd($searchText);
return "results"; //Just to see if it's returning correctly
}
$test = $request->get('txt');
var_dump($test
http://example.com/search/?txt=some%20text
试试这个
$.ajax({
headers : {
'csrftoken' : '{{ csrf_token() }}'
},
url: "{{ url("search") }}/" + encodeURIComponent(str),
type: "get", //send it through get method
success: function(response) {
console.log("ola");
$('#results').html(response);
},
error: function(xhr) {
console.log(xhr);
}
});
你可以试试这个
$.ajax({
url: "/search/",
type: "GET",
data:{txt:str},
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
路线:
Route::get('/search/', 'PrestashopController@getSearch');
控制器:
public function search($searchText){
var_dump($searchText);
return "results"; //Just to see if it's returning correctly
}
public function getSearch(Request $request){
dd($request->txt);
return "results"; //Just to see if it's returning correctly
}
你可以试试这个
$.ajax({
url: "/search/",
type: "GET",
data:{txt:str},
success: function(response) {
document.getElementById("results").innerHTML = this.responseText;
},
error: function(xhr) {
console.log(xhr);
}
});
路线:
Route::get('/search/', 'PrestashopController@getSearch');
控制器:
public function search($searchText){
var_dump($searchText);
return "results"; //Just to see if it's returning correctly
}
public function getSearch(Request $request){
dd($request->txt);
return "results"; //Just to see if it's returning correctly
}
检查控制台中的请求URL,或者控制台中是否有任何错误..?以下是控制台中的常规信息:请求URL:http://localhost/a3p/public/search?txt=ddd 请求方法:获取状态代码:404未找到远程地址:[:::1]:80检查控制台中的请求URL,或者控制台中是否有任何错误..?以下是控制台中的常规信息:请求URL:http://localhost/a3p/public/search?txt=ddd 请求方法:获取状态代码:404未找到远程地址:[:::1]:80ROUTE必须为GetRoute类型,并且为。路由::获取'/search/{$txt}','PrestashopController@search';路由的类型必须为,并且为。路由::获取'/search/{$txt}','PrestashopController@search';请帮我做一件事。。转到浏览器并键入您的url,例如app.dev/search/mystring。。告诉我你的方法是否被调用,这意味着路径不存在。但这是我的路线:路线::get'/search/{$txt}','PrestashopController@search';更新此路由::获取'/search/{txt}','PrestashopController@search';路由::获取'/search/{txt}','PrestashopController@getSearch'; 公共函数getSearch$searchText{dd$searchText;返回结果;//只是想看看返回是否正确}现在检查。。我正在更新我的答案。请帮我做一件事。。转到浏览器并键入您的url,例如app.dev/search/mystring。。告诉我你的方法是否被调用,这意味着路径不存在。但这是我的路线:路线::get'/search/{$txt}','PrestashopController@search';更新此路由::获取'/search/{txt}','PrestashopController@search';路由::获取'/search/{txt}','PrestashopController@getSearch'; 公共函数getSearch$searchText{dd$searchText;返回结果;//只是想看看返回是否正确}现在检查。。我正在更新我的答案你是对的,但它仍然给我一个404未找到。尝试从浏览器加载url,如果可以,尝试从ajax表单和浏览器的开发人员工具,检查响应。给我们新的路由代码和写入地址栏的url。现在,如果我将url直接放在浏览器中,它将调用该方法。http://localhost/app/public/search/helloYou'是的,但它仍然给我一个404未找到。请尝试从浏览器加载url,如果可以,请从ajax表单中尝试,并从浏览器的开发人员工具中检查响应。给我们新的路由代码和写入地址栏的url。现在,如果我将url直接放在浏览器中,它将调用该方法。http://localhost/app/public/search/helloI 是的,但还是没有。我更新了帖子以更正路线中的打字错误,并将其添加到请求中。我还是做了,但还是什么都没有。我更新了帖子以更正路线中的打字错误,并将其添加到请求中。仍然是404错误