简体   繁体   English

如何获取用户代理字符串并将其插入API URL以进行呼叫

[英]How to Take User Agent String and Plug Into API URL for Call

Trying to use Glassdoor API to pull JSON data and display/review. 尝试使用Glassdoor API提取JSON数据并显示/查看。

Somewhat related to this question here, except this person used Python and I need to use JS/Jquery: REST: Glassdoor API requires User-Agent in header 这里与这个问题有些相关,除了这个人使用Python并且我需要使用JS / Jquery: REST:Glassdoor API在标头中需要User-Agent

However, Glassdoor's API documentation indicates requests are intended to go through this URL/pattern: 但是,Glassdoor的API文档指示请求旨在通过以下URL /模式进行:

http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=120&t.k=fz6JLNDfgVs&action=employers&q=pharmaceuticals&userip=192.168.43.42&useragent=Mozilla/%2F4.0 http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=120&t.k=fz6JLNDfgVs&action=employers&q=pharmaceuticals&userip=192.168.43.42&useragent=Mozilla/%2F4.0

And, the user agent string returned by navigator.userAgent is much longer and formatted differently than it would need to be for inclusion in the URL: 而且,navigator.userAgent返回的用户代理字符串比包含在URL中所需的字符串更长,格式也不同:

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36 Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 51.0.2704.84 Safari / 537.36

That's what gets returned when I test it in my own setup. 这就是我在自己的设置中对其进行测试时返回的结果。 I want to make it so the client side user agent is properly added to URL for request. 我要这样做,以便将客户端用户代理正确添加到URL中以进行请求。

Do I just use the substring that is the first x number of characters? 我是否只使用前x个字符的子字符串? Is there a better way to automate how the native/client side user agent is being appended to the URL? 有没有更好的方法可以自动将本机/客户端用户代理附加到URL? OR, could I not append it to the URL and submit an object similar to the Python method that contains it and the .getJSON method will automatically do it? 或者,是否不能将其附加到URL并提交类似于包含它的Python方法的对象,.getJSON方法将自动执行此操作? I was unclear after checking the JQuery.getJSON doc. 检查JQuery.getJSON文档后不清楚。

I have tried searching the web, StackOverflow, and github for ideas or a clear JS protocol for how user agent string would be appended/parsed properly to add to URL for .getJSON method in Jquery, but I've come up blank... Or, maybe it just went over my head... 我试图在Web,StackOverflow和github上搜索想法或清晰的JS协议,以了解如何正确地附加/解析用户代理字符串以将其添加到Jquery中的.getJSON方法的URL中,但是我还是空白...或者,也许它就超过了我的头...

I'm very new and still learning, so maybe I am not looking for the right terms? 我很新,还在学习,所以也许我不是在寻找合适的用语? Or misunderstanding how .getJSON can be used? 还是误解了如何使用.getJSON?

Here's the way I have the .getJSON method and URL structured so far: 到目前为止,这是我构造.getJSON方法和URL的方式:

$.getJSON("http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=70977&t.k=e5CNsNJMxw7&action=employers&q=" + industry + "&userip=" + uip + "&useragent=Mozilla/%2F4.0")

I figured out how to use ipify.org to get client-side IP-address and drop that into the URL using the variable "uip", so that's what that is. 我想出了如何使用ipify.org获取客户端IP地址,并使用变量“ uip”将其放置到URL中的方法,就是这样。

Many thanks in advance for any help and advice you may have! 在此先非常感谢您提供的任何帮助和建议!

When using jQuery, you can provide the parameters as an object, and it will automatically format them properly into the URL: 使用jQuery时,可以将参数作为对象提供,它将自动将其正确格式化为URL:

$.getJSON("http://api.glassdoor.com/api/api.htm", {
    v: 1,
    format: 'json',
    't.p': 70977,
    't.k': e5CNsNJMxw7&action=employers,
    q: industry,
    userip: uip,
    useragent: navigator.userAgent
}, function(response) {
    ...
});

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

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