6

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).

informatik01
16.5k11 gold badges82 silver badges112 bronze badges
asked Mar 15, 2016 at 14:51
4
  • There are lots of answers already on that question stackoverflow.com/questions/1268673/… Commented Mar 15, 2016 at 14:53
  • @VitalyKulikov Thx for comment, I did some research before I post the question but seems they doesn't help. The answers in your link cant help. I shared my code in the question, hope it help. Commented Mar 15, 2016 at 14:58
  • You made a request, but you did nothing with response, that why page didn't change Commented Mar 15, 2016 at 15:05
  • @VitalyKulikov Oh, I got your point. Actually, but I am pretty new to Javascript. I tried put window.open(url) at the end and nothing happend Commented Mar 15, 2016 at 15:15

1 Answer 1

4

Here is how you can handle this:

var req = new XMLHttpRequest();
req.open('GET', 'http://example', true); //true means request will be async
req.onreadystatechange = function (aEvt) {
 if (req.readyState == 4) {
 if(req.status == 200)
 //update your page here
 //req.responseText - is your result html or whatever you send as a response
 else
 alert("Error loading page\n");
 }
};
req.setRequestHeader('header_name', 'header_value');
req.send();

answered Mar 15, 2016 at 15:19
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot, it helps me understand the use of XMLHttpRequest(). One more question, does it support adding a costumed requestheader? e.g. request header "x-api-key" which is normally appeared in every request header.
Yes, there is example in my answer req.setRequestHeader('header_name', 'header_value');
I've tried your code using window.location.href = url; to update my page. The website is navigated to url however the request header doesn't apply. Am I using a wrong way to update the page?
When you call req.send(); request is sent with appropriate header. When you call window.location.href = url; another request is sent. You need to update your page inside this statement if(req.status == 200) with simple DOM change. As soon as you use this approach, your site starts to be a single page application.
Thank you so much for clear explanation. As you mentioned req.responseText saves all message I got from the GET method from my url, I've tried document.write(req.responseText) . And the page update as my desired.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.