Ajax 调用 Laravel 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35412485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax call Laravel Route
提问by John Does Legacy
How do I properly call a Laravel Route or Controller in my Ajax?
如何在 Ajax 中正确调用 Laravel 路由或控制器?
An error appears and says:
出现错误并说:
Route [product/create] not defined. (View: C:\xampp\htdocs\laravel\resources\views\jsBlade\logoInput.blade.php) (View: C:\xampp\htdocs\laravel\resources\views\jsBlade\logoInput.blade.php)
路线 [product/create] 未定义。(查看: C:\xampp\htdocs\laravel\resources\views\jsBlade\logoInput.blade.php) (查看: C:\xampp\htdocs\laravel\resources\views\jsBlade\logoInput.blade.php)
My Routes look like this:
我的路线如下所示:
# Middleware group if user is successfully logged in
Route::group(['middleware' => 'auth'], function ()
{
Route::get('/home', ['as' => 'home', 'uses' => 'PageController@showHome']);
# Product group
Route::group(['prefix' => 'product'], function ()
{
Route::get('/', ['as' => 'indexProduct', 'uses' => 'ProductController@indexProduct']);
Route::get('new', ['as' => 'newProduct', 'uses' => 'ProductController@newProduct']);
Route::get('show/{productID}', ['as' => 'showProduct', 'uses' => 'ProductController@showProduct']);
Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
Route::post('create', ['as' => 'createProduct', 'uses' => 'ProductController@createProduct']);
Route::post('update', ['as' => 'updateProduct', 'uses' => 'ProductController@updateProduct']);
Route::delete('destroy', ['as' => 'destroyProduct', 'uses' => 'ProductController@destroyProduct']);
});
});
My Ajax:
我的阿贾克斯:
$("#input-logo").fileinput({
uploadUrl: '{{route("product/create")}}',
type: 'POST',
allowedFileExtensions: ["jpg", "png", "gif", "jpeg"],
allowedFileTypes: ['image'],
headers: {
'X-CSRF-Token': $('#_token').val(),
}
}).on('filepreupload', function() {
$('#kv-success-box').html('');
}).on('fileuploaded', function(event, data) {
$('#kv-success-box').append(data.response.link);
$('#kv-success-modal').modal('show');
});
</script>
Controller
控制器
<?php
namespace App\Http\Controllers;
use Input;
use App\Product;
use App\Companies;
use App\Http\Controllers\Controller;
class ProductController extends Controller
{
public function createProduct()
{
$data = Input::all();
$product = new Product;
$product->fill($data);
if($product->save())
{
return redirect()->route('root')->with('message','Success');;
}
}
}
Firefox gives this error message:
Firefox 给出了这个错误信息:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
SyntaxError: JSON.parse: JSON 数据的第 1 行第 1 列出现意外字符。
回答by paranoid
Change this part
改变这部分
uploadUrl: '{{route("product/create")}}',
to this
对此
uploadUrl: '{{url("product/create")}}',
and add a csrf token to your header in ajax
并将 csrf 令牌添加到 ajax 中的标头
headers: {
'X-CSRF-Token': '{{ csrf_token() }}',
},
回答by Pedram Vdl
Just Ajax to the URL of the route like this:
只需 Ajax 到路由的 URL,如下所示:
This is my route:
这是我的路线:
Route::post('users/send-file-temp',['uses'=>'UsersController@postSendFileTemp']);
and here is my ajax call:
这是我的ajax调用:
$.ajax({
url: '/users/send-file-temp',
});
Ajax will send the request to /users/send-file-tempand Laravel will recognize the route and direct it to the corresponding controller.
Ajax 将请求发送到/users/send-file-tempLaravel 将识别路由并将其定向到相应的控制器。
回答by Don Ejeh
Take not of your ajax method typeand your Route methodtype
不考虑您的 ajax方法类型和Route 方法类型
Example below
下面的例子
My Ajax code
我的 Ajax 代码
$('#sendMsg').click(function(e){
e.preventDefault();
$.ajax({
url: '{{url("R_E/users/sendMSG")}}',
data: $("#form-signin").serialize(),
type: "POST",
headers: {
'X-CSRF-Token': '{{ csrf_token() }}',
},
success: function(data){
alert("okay");
},
error: function(){
alert("failure From php side!!! ");
}
});
});
My route Code
我的路线代码
Route::post('/users/sendMSG', 'RE\MainController@sendMSG');
Route::post('/users/sendMSG', 'RE\MainController@sendMSG');

