问题描述
因此,我试图理解laravel中的路由,恐怕没有太大的成功.我当前的问题是我正在尝试进行路由,以便可以从站点上的所有页面访问使用ajax检查的登录功能.目前,我只能从索引中达到目标.我使用以下
So I'm trying to understand routing in laravel, without much success I'm afraid. My current issue is that I am trying to route so that my login function, which is checked using ajax, is accessible from all pages on the site. At the moment i can only reach it from my index. i use the following
Route::post('login', array('uses' => 'LoginController@doLogin'));
和以下ajax:
$.ajax({
type: "POST",
url: "<%= ResolveUrl("~/login") %>",
data: "username="+username+"&password="+password,
success: function(data) {
console.log(data)
if(data == 'Fel användarnamn eller lösenord.') {
document.getElementById('loginerror').innerHTML = data
} else if (data == 'Inloggad'){
document.getElementById('loginerror').innerHTML = data;
window.location.reload(true);
}
现在,据我了解,解析URL可能会解决我的问题.但是,无论我操作url部分,我都无法解决问题.我只会收到语法错误或错误的请求错误.如果laravel中内置了此功能,以便获得独立于URL更改的正确路由,我将不胜感激此类功能的任何提示.我在文档中找不到/能够解决我的问题的任何内容.否则,如果有人可以告诉我如何正确编写ResoleUrl,也将不胜感激.但是,对laravel路由的了解可能会更好地为我服务!预先感谢!
Now it is to my understanding that resolving the URL would possibly solve my issue. However, no matter i manipulate the url part, I cant get the resolve to work. I only get syntaxerrors or bad request errors. If there's something built in into laravel for this, to get the correct route independently of changes in the URL, I would greatly appreciate any tip of such functionality. I cant find/understand anything in the documentation that would solve my problem. Else if anyone could tell me how to write the ResoleUrl correctly, that would also be greatly appreciated. however an understanding of laravels routing would probably serve me better! Thanks beforehand!
推荐答案
将ajax中的URL更改为:
Change the url in your ajax to:
url:"/login"
这篇关于在Laravel中路由,在Ajax中解析URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!