Simple XML HttpRequest question - javascript

I'm trying to launch a request for some data from an external website w/ an API.
If I copy paste the request into my web-browser it works fine.
Example such as (http://example.com/json/user/search/all)
I can see the results in the browser window.
However, I'm trying to launch this query from a website and I'm running into an issue:
Using some javascript like this:
var req = new XMLHttpRequest();
req.open('GET', abovementionedurl, true)
req.send()
I get an error that reads: Origin (MyDOMAIN) is not allowed by Access Control Allow Origin.
Again, I'm fairly new to XMLHttpRequests and I think this is fairly simple to solve.
Anyone know? (I tried searching google but couldn't find a good answer)
Thanks in advance.

You are not allowed to do Cross-Site XMLHttpRequests and need to use a proxy to accomplish it.
This article from Yahoo explains it in detail and gives more ideas of how to solve it. But basically it should not be done because this restriction exists for security purposes.
It would be better if you used PHP and fopen() for instance and simply called that page using your XMLHttpRequest object.

The problem is the same-origin policy. This is a rule that XMLHTTPRequests may not be used except on the same domain as the original page. This is for security reasons.
The easiest way around it is to write a server-side script that proxies the request for you.

This is your browser's protection against cross side scripting. You are not allowed to access other pages that come from a different domain.
EDIT: Also check this: Cross-site XMLHttpRequest

Related

How does DownForEveryoneOrJustMe.com and other similar sites work with Cross Domain Scripting restrictions?

I would like to make a simple website that allows the user to enter a URL, click a button, and if the URL loads successfully, alert the user. Something like the site http://www.downforeveryoneorjustme.com/ (or for a fancier version http://www.isitdownrightnow.com/).
Javascript, specifically XmlHttpRequest(), does not seem to be an option due to Cross-Domain Scripting restrictions. A similar issue occurs through use of hidden iframes or windows and the onLoad() event.
My question is - how do these sites work given the restrictions on cross-domain scripting? What's the key piece of technology I'm missing here?
These sites get around the restrictions on cross domain scripting by not making their requests using the browser - they don't load the URL on the client side. Instead, the user sends a request to the webserver, the webserver runs a server-side script to try and access the provided URL, and informs the user if its attempt was successful.
The question they're answering is "Can someone other than me access this site?" Making the person asking the question try and access the site wouldn't answer that question.

XMLHttpRequest cross site scripting?

I realize this issue of cross site scripting has been covered, however being new to web development I had a few further questions.
Currently I am testing an html file I wrote on my PC connecting to a RESTFul web service on another machine. I am getting status=0. Is this considered cross-site scripting?
If a server hosts a file with javascript, and that javascript file has XMLHttpRequests to the server's own web services, will that work, or is that bad?
Apologies if any of these questions are stupid.
status=0 can me a variety of things, and without knowing more about how you got to that point, it is very difficult to determine what, exactly, it means. You could be using an iframe, the other computer could genuinely be telling you that the status is 0... we don't know.
The general rule is that it doesn't matter where the JS is from, it will execute the data where it's loaded. This is what makes the Google js archiving api possible (you know, use https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js on a whole assortment of locations). And honestly, that is not a security issue.
The security issue comes in when a js file tries to access another domain (or even subdomain), whether through manipulation of an iframe or through XMLHTTPRequest. It's at that point that the browser will "lay the smackdown" on the script.
You will have difficulty communicating with JavaScript from your hard drive (file:///) to any internet protocol (http|https) because of this.
No, that is not cross site scripting. When including script JS file from another server it is rendered in your site so You won't be able to access through XMLHttpRequest site where JS script is originally located.
If that is possible than anybody who host jQuery file, there are many servers including google, would be opened for XMLHttpRequests.
SO, IT'S NOT POSSIBLE.
If you want JSON response from another server you can use pjson. Google it for more info.
And Cross Site Scripting is when someone injects JavaScript code on your site in order to bypass access control.
You can use CORS for that. You can use the same code you use now, but the other server you request the page from via ajax has to sent the following header on that page
Access-Control-Allow-Origin: http://yoursite.example.com
#or to allow all hosts
Access-Control-Allow-Origin: *

How to specify an external website for XMLHTTPRequest

When using an XMLHTTPRequest in javascript, I want to send it to an external website, rather than the one where the .js file is hosted. To send it to test.php on the current server, I would use
request.open("POST", "test.php", true);
but for the second arguemnt, how do I send it to another website. "example.com/test.php" looks for a file on the current server, and "http://example.com/test.php" justseems to outright fail.
You can't for security reasons. See the same origin policy for JavaScript.
There are some workarounds that exploit browser bugs or corner cases, but using them is not recommended.
The best approach is having a server-side proxy that receives Ajax requests, and in turn, sends HTTP requests to other servers. This should be carefully implemented by sanitizing input and whitelisting the types of requests that are sent, and the servers that are contacted.
This sounds like a bad case of Same Origin Policy, my friend :)
You can't (for the most part) use XmlHttpRequest to get data from an external website. What you can do, however, is dynamically create a SCRIPT tag and reference an external address. jQuery wraps this functionally as part of its ajax handling.
Indeed you can. Not in any browser although.
In Internet Explorer 8.0 there is XDomainRequest, an object enabling cross-domain requests. You would need to properly handle request made with this object on server by sending Access-Control-Allow-Origin header first with "*" or requester domain name.
Since you are doing some hacky things anyway, why not trying to use it on IE8 first?
If you have control over the server, you can use this header in the HTTP reply, although it may not work with all browsers.
Access-Control-Allow-Origin: *

java script, XMLHttpRequest, permission denied within browser

my js file calls uses an xmlHttpRequest to display an rss feed. when i reference the java script within my html page, my browser renders an error: Permission denied. and therefore, my script is being blocked. i am not allowed to change the security settings and would i'd appreciate any work around tips. thanks!
It's called the same origin policy. There's no easy workaround.
Simply put, XmlHttpRequest doesn't allow you to perform ajax calls across domains. Meaning, if you're website is mydomain.com, you can't use XHR to call out to pages (xml or otherwise) on someonelsesdomain.com.
There are work-arounds, typically using flash (less elegant) or webservice proxies (more elegant). Google "cross domain ajax calls" for more help.
Cross-origins call? You can't do that ( at least not directly e.g. need an extension/plugin etc.).
You need a proxy server... a utility that will allow access to foreign (in this case, on another server) material on your own server. Since (as the others mentioned) Browsers have security features enabled to prevent you from accessing content via AJAX on these foreign servers, a proxy will enable you access to this content locally.
this .net method was perfect and easy to implement: http://www.asp101.com/articles/john/megatokyo/dotnet.asp

Can I make an XMLHttpRequest to another domain?

Is there a way to use XMLHttpRequest in combination with other domains?
I would like to parse some xml from Google without having to use a server so it is minimalistically complex to run.
var req = getXmlHttpRequestObject();
...
req.open('GET', 'http://www.google.de/ig/api?weather=Braunschweig', true);
req.setRequestHeader("Content-Type","text/xml");
req.onreadystatechange = setMessage;
req.send(null);
Doing it on the server side is no option at least then I wouldn't have to ask
Nope, not right now. I believe I read that plans/design's are in the works by standards groups for the future, so we can securely do this.
Cross site scripting vulnerabilities would be rampant other wise.
JSONP is a possible solution if the other sites API supports.
HTML5 now supports Cross Origin requests with XmlHttpRequest level 2 take a look at :
http://www.html5rocks.com/en/tutorials/cors/
It's a security issue, most (all?) browsers won't let you do that. You can use a hidden IFrame to do your fetching, but it's complex enough that i'd just use a server (or switch to a different language, if i don't have to run in a browser)
You cannot do cross domain request,e.g. from example1.com to example2.com through XMLHttpRequest or jQuery(which is a wrapper of XMLHttpRequest) due to security issue in client side(browser). This can be effectively implemented in modern browser supporting HTML5 through CORS(cross origin resource sharing, which cannot be available in every client browser. So, the solution is to insert script tag in example1.com of example2.com, and this solution is known as JSON-P(JSON with padding), the name could be misleading as the data can be in any format served by the server(example2.com). Its implementation code is given in this link http://newtechinfo.net/jsonp-for-cross-domain-ajax/
That's not possible due to the SOP (Same Origin Policy) that browser have these days to restrict XSS attacks.
You will have to use a server side script (PHP or something).
It is possible to make a XHR to another domain with HTML5.You can also make different protocol request with XHR when communicating with HTTP to another web site.
You can try to do something on the serverside. So on your application you make the request to the remote site getting the result and returning it to your client. The AJAX call is then only calling your own server and works.

Categories

Resources