0

Python has a module called httplib which allows for the retrieval of an html resource from a URL. With this code:

httpServ = httplib.HTTPConnection("www.google.com")
httpServ.connect()
httpServ.request('GET',"/search?q=python")
...
httpServ.close() 

I am trying to do the same thing in my angular app, but using $http get doesn't allow me to retrieve the html document due to the same origin policy.

Is there anything similar to the python method available in JavaScript?

2
  • Yes, but if the page is from a different origin, you will see the same origin policy error. Commented Feb 8, 2016 at 18:23
  • That's what I need to get around. I think that this might be possible through the server end with nodejs, but I have never done that. Commented Feb 8, 2016 at 18:27

1 Answer 1

1

So, the Same-Origin Policy has nothing to do with JavaScript. It basically says "don't allow scripts on a page to talk to scripts being run by another host."

This is an extremely important security feature. It means that if you put jQuery on your page, and somehow a jQuery CDN got hacked and they changed jQuery to send your passwords to another page, it wouldn't work (so long as the browser properly enforces the Same-Origin Policy).

You don't have this problem when working with Python because Python exclusively runs on the server (from a web-app perspective). Your server can talk to any machine it wants to, but browsers do not (and should not as seen above) give that freedom to webpages.

So, how to solve your problem? Make your GET request to a script running on your server. Have your server do a curl or wget or w/e of google.com, then have your server send the data back to the client.

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

3 Comments

Thank you @Matthew Herbst, your explanation makes it clear why this is impossible in the front end, and should be done on the server side. Now I just need to see how to do this with NodeJS.
It would seem that there is a curl wrapper available for node, npmjs.com/package/node-curl so I will start there.
Yeah, there are tons and tons of packages that will make this very easy for you.

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.