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.

3
  • Read this: stackoverflow.com/questions/5771878/… Commented Oct 29, 2012 at 18:38
  • Or this: stackoverflow.com/questions/10093053/… Commented Aug 26, 2013 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. Commented Feb 16, 2017 at 17:59

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

From the docs it says "Aditional headers", so someone can guess it is only for not already-defined headers, but in fact, that works.
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"]);

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.