Pepepapa82
Pepepapa82

Reputation: 13

How to add a value to User-Agent header in a .ajax request?

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.

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");
    }
});

But it doesn't work.

I also tried:

$.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
});

Which isn't working either.

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.

Upvotes: 0

Views: 13123

Answers (3)

m3nda
m3nda

Reputation: 2017

Considerations:

  • U have to open the html from the webserver, not the file:/// handler.

  • U have to ensure that you didn't add typos, like leave the User-Agent string quotes opened.

  • Remove/edit type: "POST" in case you want to make GET request.

  • There are 2 ways to edit headers, using BeforeSend and using headers. Both of them works with $.ajax() and $.post() methods.

Example 1:

$.post({
    url: "http://localhost:4000", 
    data: "we=1",
    headers: {"User-Agent": "user agent 1"}
    });

Example 2:

$.ajax({ 
    url: "http://localhost:4000",
    type: "POST",
    data: {we: "2"},
    dataType:'text', // case you wanna especify the return type
    headers: {"User-Agent": "user agent 2"}
});

Using headers seems more easy and maybe is is just a shorcut to BeforeSend method:

$.ajax({ 
    url: "http://localhost:4000",
    type: "POST",
    data: {we: "2"},
    dataType:'text',
    headers: {"User-Agent": "user agent 1"}
    beforeSend: function (req) {
        req.setRequestHeader('User-Agent', 'user agent changed to 2');
    }
});

In case you wanna check for results... add somewhat like that:

error: function() { alert("No data found");},
success: function() { alert("uh yead");}

But using the Firefox/Chrome dev tool (f12 -> network tab) is good enough to check it out.

Regards.

Upvotes: 1

tomdemuyt
tomdemuyt

Reputation: 4592

You can use headers[], that is easier than using beforeSend. Just Ctrl-F 'headers' here.

Upvotes: 2

Fedir RYKHTIK
Fedir RYKHTIK

Reputation: 9974

If I understood correctly Your question, You could use webRequest Chrome API. "Use the chrome.webRequest module to intercept, block, or modify requests in-flight and to observe and analyze traffic.

http://developer.chrome.com/trunk/extensions/webRequest.html

There is an example there how to remove the User-Agent. Instead to remove, You could change the value of requestHeader with Your value.

 chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    for (var i = 0; i < details.requestHeaders.length; ++i) {
      if (details.requestHeaders[i].name === 'User-Agent') {
        details.requestHeaders.splice(i, 1);
        break;
      }
    }
    return {requestHeaders: details.requestHeaders};
  },
  {urls: ["<all_urls>"]},
  ["blocking", "requestHeaders"]);

Upvotes: 1

Related Questions