从 Laravel 控制器调用 Javascript 函数并返回数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37879258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:00:39  来源:igfitidea点击:

Calling Javascript function from Laravel controller and return data

javascriptlaravellaravel-5firebase-authentication

提问by Chaudhry Waqas

I want to call a firebaseauth function (which is in Javascript) from my controller and I want the returned tokenback in my controller, but I do not know how to proceed.

我想从我的控制器调用firebaseauth 函数(在 Javascript 中),我想token在我的控制器中返回,但我不知道如何继续。

I know how to post ajaxrequest to controller and get data back, however I don't know how to make a request the other way around.

我知道如何向ajax控制器发布请求并取回数据,但是我不知道如何以相反的方式发出请求。

I would be thankful if you can put me and other future users in right direction.

如果您能让我和其他未来的用户朝着正确的方向前进,我将不胜感激。

回答by Randy

This is not going to work.

这是行不通的。

You know a webserver is always supposed to be online. That way, you can always make a request.

您知道网络服务器应该始终在线。这样,您始终可以提出请求。

A client-side (user), does notneed, and isn't always online. If they close the browser window, you can't interact with them anymore. The server would have a hard time checking what user just went down, out of the thousands of users you have.

一个客户端(用户),也没有需要的,是不是永远在线。如果他们关闭浏览器窗口,您将无法再与他们互动。服务器将很难从您拥有的数千个用户中检查哪个用户刚刚宕机。

Making a call to a webserver is possible, but making a call to a client is not.

可以调用 Web 服务器,但不能调用客户端。

There are, next to that reason, security issues if you would be able to make a request to the client-side.

除此之外,如果您能够向客户端发出请求,还有安全问题。

What you should probably do, is make a seperate request for the authentication token from Javascript, make Laravel call the Firebase AUTH server and then return the token it received.

您可能应该做的是,从 Javascript 对身份验证令牌发出单独的请求,让 Laravel 调用 Firebase AUTH 服务器,然后返回它收到的令牌。



update

更新

It is possible to make requests to a client using HTTP2, by using server-push. To do that properly, especially with an authentication token, you will need some experience in building webservices and security.

通过使用server-push,可以使用 HTTP2 向客户端发出请求。要正确地做到这一点,尤其是使用身份验证令牌,您将需要一些构建 Web 服务和安全性的经验。

回答by Rahul M

PHP is a server side language and Javascript is client side. You cannot javascript on server and vice-versa. If you just need to generate JWT token, there are PHP libraries available for that already. But if you really want to execute javascript on server, that I think is not the case, you'll have to use nodejs

PHP 是服务器端语言,Javascript 是客户端。您不能在服务器上使用 javascript,反之亦然。如果您只需要生成 JWT 令牌,那么已经有可用的 PHP 库了。但是如果你真的想在服务器上执行 javascript,我认为情况并非如此,你将不得不使用nodejs