Laravel 4 + AJAX不工作 [英] Laravel 4 + AJAX not working
本文介绍了Laravel 4 + AJAX不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很新的Laravel 4,我试图测试Ajax请求。
I am very new to Laravel 4 and I'm trying to test AJAX request.
在我的的script.js
我有这样的:
(function() {
$("#login-submit").click(function(e) {
e.preventDefault();
return $.ajax({
type: "POST",
url: "laravel/ajax/login", //i put my public in "localhost/laravel/public/"
cache: false,
data: 'email:' + $("#email").val(),
success: function(data) {
return alert(data);
},
error: function(response) {
return alert("ERROR:" + response.responseText);
}
});
});
}).call(this);
这是从这个HTML文件名为:
That is called from this html file:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<label for="email">Email: </label>
<input type="text" id="email" />
<label for="email">Password: </label>
<input type="password" id="password" />
<button id="login-submit">Log In</button>
</form>
<script type="text/javascript" src="assets/js/jquery/jquery.custom.js"></script>
<script type="text/javascript" src="assets/js/script.js"></script>
</body>
</html>
Controller类:
Controller class:
class AuthController extends BaseController
{
public function login()
{
echo 'success';
}
}
和路由器:
Route::get('/', function() {
return View::make('homepage');
});
Route::post('/ajax/login', 'AuthController@login');
点击登录
应该呼应的成功,而是它抛出这个错误:
By clicking Log In
it should be echoing "success" but instead it throws this error:
ERROR:{
"error": {
"type": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"message": "",
"file": "C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
"line":1429
}
}
有什么错呢?
What's wrong in this?
推荐答案
在AJAX调用,更改:
Within the AJAX call, change:
url: "laravel/ajax/login",
要:
url: "ajax/login",
您的路线是听 AJAX /登录
,不可以 laravel / AJAX /登录
。
了解更多关于路线这里。
这篇关于Laravel 4 + AJAX不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文