Navigate to URL with custom request headers in JavaScript

Viewed 10960

Question just like the title.

In command line, we can type:

curl -H "header_name: header_value" "http://example"

to navigate to http://example with a custom request header as shown above.

Q: If I need to write a JavaScript to do the same thing, how should I do?

var url = 'https://example';
var myRequest = new XMLHttpRequest();
myRequest.open('GET', url ,false);
myRequest.setRequestHeader('header-name','header-value');
myRequest.send();

I tried this code, there is no syntax error but the page didn't change. Hence, I don't really know if I modified the request header(s).

1 Answers
Related