如何在 .ajax 请求中向 User-Agent 标头添加值? [英] How to add a value to User-Agent header in a .ajax request?
问题描述
我正在尝试为 Chrome 创建一个插件,我可以在其中从多个页面检索信息,其中一些页面具有负载平衡器,需要特定的用户代理代码才能将我路由到正确的位置.
I'm trying to create a plugin for Chrome where I retrieve info from several pages, some of them they have a load balancer and need a specific user agent code to route me to the correct place.
现在,我正在执行 .ajax()
调用,并且我尝试了一些方法,例如:
Now, I'm doing an .ajax()
call, and I've tried a couple of things such as:
$.ajaxSetup({
beforeSend: function(request) {
request.setRequestHeader("User-Agent","MyAgentCode");
}
});
但是它不起作用.
我也试过:
$.ajax({
url: "http://blablabla.com/",
dataType:'html',
beforeSend: function (req) {
req.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1 MyAgentCode);
},
error: function() { alert("No data found");},
success: parseResult
});
这也不起作用.
我只想向 User-Agent
添加一个值(保留其余部分).这将使我能够从正确的服务器获取正确的信息.
I only want to add a value to the User-Agent
(keeping the rest as it is). This will allow me to get the correct information from the correct server.
推荐答案
您可以使用 headers[],这比使用 beforeSend 更容易.只需 Ctrl-F 'headers' 此处.
You can use headers[], that is easier than using beforeSend. Just Ctrl-F 'headers' here.
这篇关于如何在 .ajax 请求中向 User-Agent 标头添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!