简体   繁体   English

跨站点Ajax调用

[英]Cross-Site Ajax Call

I am able to execute a web service through the browser but when I try and execute it through xmlhttprequest in javascript I get this error: Origin [] is not allowed by Access-Control-Allow-Origin. 我能够通过浏览器执行Web服务,但是当我尝试通过javascript中的xmlhttprequest执行它时,我收到此错误:Access-Control-Allow-Origin不允许Origin []。

How can I call the webservice through javascript? 如何通过javascript调用网络服务? I'd prefer not to use a framework and just use basic client-side javascript. 我不想使用框架,只是使用基本的客户端JavaScript。

For reference this is an instance of the web service I'm trying to consume: http://musicbrainz.org/ws/1/track/?type=xml&query=track:alive 作为参考,这是我正在尝试使用的Web服务的一个实例: http//musicbrainz.org/ws/1/track/? type = xml&query = track: folive

Thanks. 谢谢。

Not possible. 不可能。 You can't have an AJAX call to a different domain. 您无法通过AJAX调用其他域。 It would need to be done by the server. 这将需要由服务器来完成。

You cannot use XMLHttpRequest on a page hosted at one domain to retrieve information from another domain. 您不能在托管在一个域的页面上使用XMLHttpRequest来从另一个域检索信息。 The browser simply won't allow it. 浏览器根本不允许它。

One common workaround is quite reliable if you have some control over the resource you're retrieving. 如果您对要检索的资源有一定的控制权,则一种常见的解决方法是非常可靠的。 It's called "JSONP" and the technique is to simply append a <script> tag to the header (dynamically, using JavaScript). 它被称为“JSONP”,技术是简单地将<script>标记附加到标题(使用JavaScript动态地)。 That script can of course be hosted on any domain, so there's no cross-site scripting restrictions. 该脚本当然可以托管在任何域上,因此没有跨站点脚本限制。 If the script were to consist simply of JSON data, it wouldn't do much good. 如果脚本仅由JSON数据组成,那将不会有什么用。 But wrap that JSON data in a function call — a function you control on your side — and it works great. 但是将JSON数据包装在一个函数调用中 - 一个你控制的函数 - 它运行得很好。

someFunctionName( { ... } );

If the resource you're retrieving doesn't support JSONP, your only recourse is to write a script on your own server (hosted on the same domain as the page, of course) that retrieves the target data. 如果您要检索的资源不支持JSONP,那么唯一的办法就是在您自己的服务器(当然,该主机与页面位于同一域)上编写一个脚本,以检索目标数据。 You can then make a normal AJAX call to your own script. 然后,您可以对您自己的脚本进行正常的AJAX调用。

XmlHttpRequest is for requests for the same domain - it is called "Same origin policy". XmlHttpRequest用于同一域的请求 - 它被称为“同源策略”。 If you want to do cross-domain ajax request you have to use jsonp format. 如果要执行跨域ajax请求,则必须使用jsonp格式。 I do not think that using plain JavaScript is good idea - much better solution is ready-to-use framework eg jQuery or Mootools. 我认为使用纯JavaScript不是一个好主意-更好的解决方案是现成的框架,例如jQuery或Mootools。 In jQuery you have got method $.getJSON to making simple JSON and JSONP requests. 在jQuery中,你有方法$ .getJSON来制作简单的JSON和JSONP请求。

More read about: How exactly is the same-domain policy enforced? 详细了解: 如何严格执行同域策略? http://api.jquery.com/jQuery.getJSON/ http://en.wikipedia.org/wiki/JSON#JSONP http://api.jquery.com/jQuery.getJSON/ http://en.wikipedia.org/wiki/JSON#JSONP

NOT Possible, but you can create a proxy on your server, and access the data through the proxy: 不可能,但是您可以在服务器上创建代理,然后通过代理访问数据:

Here is a proxy example in PHP: 这是PHP中的代理示例:

proxy.php proxy.php

<?php
    //header('Content-type: text/xml');
    $url = 'http://musicbrainz.org/ws/1/track/?type=xml&query=track:alivehttp://www.example.com/';
    $xml = file_get_contents($url);
    echo xml;
?>

You can now use proxy.php as your source url from javascript. 您现在可以使用proxy.php作为来自javascript的源URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM