0

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.

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
Pepepapa82
  • 13
  • 1
  • 1
  • 3
  • Read this: http://stackoverflow.com/questions/5771878/jquery-ajax-request-change-user-agent – ericforgues Oct 29 '12 at 18:38
  • Or this: http://stackoverflow.com/questions/10093053/add-header-in-ajax-request-with-jquery – Rose Perrone Aug 26 '13 at 22:33
  • 1
    For sure `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);` will not work. Ur missing the closing `'` at User-Agent string. – m3nda Feb 16 '17 at 17:59

3 Answers3

2

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

tomdemuyt
  • 4,572
  • 2
  • 31
  • 60
  • From the docs it says "Aditional headers", so someone can guess it is only for not already-defined headers, but in fact, that works. – m3nda Feb 16 '17 at 19:00
1

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"]);
Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68
1

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.

m3nda
  • 1,986
  • 3
  • 32
  • 45