简体   繁体   English

如何从angularjs中的网址中删除api_key

[英]How to remove api_key from url in angularjs

I need an apikey to be able to send a get request to a REST api. 我需要一个apikey才能将get请求发送到REST api。 The request will look something like this - 该请求将如下所示-

$http.get( http://api.randomsite.com/page.json?api_key=apikey123 ) $ http.get( http://api.randomsite.com/page.json?api_key=apikey123

This will give me data in json format. 这会给我json格式的数据。

But i dont want the api key to appear in the url due to safety reasons. 但是出于安全原因,我不希望api键出现在url中。 Is it possible to remove the api_key and somehow still make the get request work? 是否可以删除api_key并以某种方式仍然使get请求起作用? Like this - 像这样 -

$http.get( http://api.randomsite.com/page.json ) $ http.get( http://api.randomsite.com/page.json

I read its something to do with headers, but i tried a lot and cannot figure it out. 我读了一些与标题有关的内容,但尝试了很多,却无法弄清。 Help :/ 救命 :/

Well, if you want to hide the API_KEY, there's few ways to do this 好吧,如果您想隐藏API_KEY,则有几种方法可以做到这一点

  1. Send it through POST , although this is only a half measure, as anyone could look into the POST body and see what it is. 通过POST发送它,尽管这只是一半,因为任何人都可以查看POST正文并查看它是什么。 Nevertheless, the way you do it is by 不过,您的做法是

$http.post("api/salesapi.php/sales/", {API_KEY:xx}, {headers: ... });

  1. Let your server handle it; 让您的服务器来处理它; This hides the API_KEY from the user completely and let the server act as the middle man. 这将对用户完全隐藏API_KEY,并让服务器充当中间人。 But, you need a server to do this, so that's not possible if you're server-less 但是,您需要一台服务器来执行此操作,因此如果您没有服务器,那是不可能的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM