【发布时间】:2016-08-10 17:25:57
【问题描述】:
我在html 中呈现我的观点,并在laravel 中拥有我的后端api。用户从 cloudapp.azure.com/welcome.html 登录后,我的脚本 cloudapp.azure.com/js/script2.js 将用户密码发送到我的 Laravel 控制器;
//cloudapp.azure.com/js/script2.js
if(error_username == false && error_password == false){
var details = [$("#username1").val(),$("#password1").val()];
var url = "http://depressionapp1.westeurope.cloudapp.azure.com/index.php/";
$.ajax({
url: url,
type: 'POST',
data: {dataname: details},
success: function(result){
//THIS IS WHERE I AM HAVING PROBLEMS
location.href = "http://depressionapp1.westeurope.cloudapp.azure.com/welcome1.html";
/* or
var url1 = "http://depressionapp1.westeurope.cloudapp.azure.com/" + result;
$.get(url1);*/
}
});
来自路由 url cloudapp.azure.com/index.php/ 的我的控制器 处理请求并根据需要返回字符串“welcome.html”。
//UserController@postLogin works fine and returns 'welcome.html'
public function postLogin(Request $request){
$result = DB::table('users')->select('user_id')->where('username', $request['username'])->get();
$user = $result[0];
$val = '';
if(Auth::attempt(['username' => $request['username'], 'password' => $request['password']])){
$count = Answer::where('user_id', $user->user_id)->where('level', '<>', NULL)->count();
if($count){
return redirect()->route('checkLevel');
}else{
$val = 'welcome1.html';
}
}
$data = array('page' => $val, 'id' => $user->user_id);
return response($val);
}
现在,因为 Laravel 处理“welcome.html”返回的 url 路由是 cloudapp.azure.com/index.php/ 它似乎总是留在这个 url 中,并且在我的 ajax 调用成功后,我想重定向到cloudapp.azure.com/welcome1.html 的 html 页面,即。 'cloudapp.azure.com' + 结果(即 'welcome.html')。
我已尝试关注
$.ajax({
url: url,
type: 'POST',
data: {dataname: details},
success: function(result){
//THIS IS WHERE I AM HAVING PROBLEMS
location.href = "http://depressionapp1.westeurope.cloudapp.azure.com/welcome1.html";
/* or
var url1 = "http://depressionapp1.westeurope.cloudapp.azure.com/" + result;
$.get(url1);*/
}
但我似乎卡在 Laravel 的路线上,无法重定向。
【问题讨论】:
-
你试过了吗
window.locationstackoverflow.com/a/506004/1133306 -
是的,如上图; location.href = depressionapp1.westeurope.cloudapp.azure.com/welcome1.html"; 没用。
-
你的
success函数中result的内容是什么? -
它是一个字符串'welcome.html',如上所示。
-
在得到 Laravel 的响应后,我遇到了类似的重定向问题。
location的一些变体对我有用。这就是为什么我问你是否尝试过window.location=,尽管它基本上是一样的。见developer.mozilla.org/en-US/docs/Web/API/Window/location