jQuery 访问受限 URI 被拒绝”代码:“1012 - 跨域 Ajax 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1681470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to restricted URI denied" code: "1012 - Cross domain Ajax request
提问by Shaitender Singh
I Need to do cross domain Ajax request - Here is my code
我需要做跨域 Ajax 请求 - 这是我的代码
$.ajax(
{
url: redirectURL,
data: $('#login-container form').serialize() + querystring,
type: 'post',
cache: false,
dataType: 'jsonp',
jsonp: 'jsonp_callback',
});
Error: [Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: "http://testsite/assets/scripts/jquery-1.3.2.js Line: 19"]
Source File: http://testsite/assets/scripts/jquery-1.3.2.js
Line: 19
I have checkout the following links too -
我也结帐了以下链接-
Access to restricted URI denied code: 1012
$.ajax(
{
url: redirectURL+'?callback=?',
data: $('#login-container form').serialize() + querystring,
type: 'post',
cache: false,
dataType: 'html' });
I have tried Callback in url too . I had already seen all link in stackoverflow regarding this issue.. but not able to overcome this thing Can anyone please help and tell me how to overcome !! Thanks
我也尝试过在 url 中回调。我已经在stackoverflow中看到了关于这个问题的所有链接..但无法克服这个问题任何人都可以帮助并告诉我如何克服!谢谢
回答by Shimon Rura
You won't be able to do a cross-domain POST request in the browser.
您将无法在浏览器中执行跨域 POST 请求。
If you are making a JSONP call to access a cross-domain URL, you can use JQuery's getJSONmethod. This would allow you to make a GET request only. If you can submit your login information to the redirectURL using GET parameters, you could make this work.
如果您要进行 JSONP 调用以访问跨域 URL,则可以使用 JQuery 的getJSON方法。这将允许您仅发出 GET 请求。如果您可以使用 GET 参数将您的登录信息提交到 redirectURL,您就可以完成这项工作。
Note that POSTing to remote login forms is perhaps the best example of why browsers disallow cross-domain requests like this. You don't want a page that looks like your bank to be able to actually serve you data from your bank's website -- that would make a very effective phishing page.
请注意,POST 到远程登录表单可能是浏览器禁止此类跨域请求的最佳示例。您不希望看起来像您的银行的页面能够实际为您提供来自银行网站的数据——这将成为一个非常有效的网络钓鱼页面。
On the other hand, if you really want to work around this, you can write some server-side code that, given the input parameters, makes a post request to the redirectURL and funnels back the response.
另一方面,如果你真的想解决这个问题,你可以编写一些服务器端代码,给定输入参数,向 redirectURL 发出 post 请求并返回响应。
Please tell me you are not writing a phishing page.
请告诉我您不是在编写网络钓鱼页面。
回答by Dapeng
One way to do it is to use your server as a proxy:
一种方法是使用您的服务器作为代理:
AJAX Request -> Your server -> Server of interest
AJAX Response Handler <- Your server <- Server of interest
回答by Josef Pfleger
JSONP is not really Ajax in a way that it does not use XMLHttpRequest
but a dynamic script
element. jQuery makes this transparent but JSONP still requires the following to work:
从某种意义上说,JSONP 并不是真正的 Ajax,XMLHttpRequest
而是一种动态script
元素。jQuery 使这变得透明,但 JSONP 仍然需要以下内容才能工作:
- HTTP
GET
only, you can't usePOST
s - the server needs to support it by wrapping the response in a call to the
callback
you specify
GET
仅限HTTP ,不能使用POST
s- 服务器需要通过将响应包装在对
callback
您指定的调用中来支持它
Check if the URL you request supports JSONP and change the call to an HTTP GET
.
检查您请求的 URL 是否支持 JSONP 并将调用更改为 HTTP GET
。
回答by Boldewyn
If you do control the remote server, too, in recent browsers (IE8, FF3.5) you can overcome the cross-domain obstacle:
如果你也控制远程服务器,在最近的浏览器(IE8、FF3.5)中你可以克服跨域障碍:
IE's XDomainRequest methodand the W3C's and Mozillas Origin-Header.
IE 的 XDomainRequest 方法以及W3C 和 Mozillas 的 Origin-Header。
In short, if your server sends back the HTTP header
简而言之,如果您的服务器发回 HTTP 标头
Access-Control-Allow-Origin: *
with the AJAX response, IE and Mozilla will accept it, even if the request stems from a different domain. You can narrow down allowed requesting domains by replacing the *
with them.
有了 AJAX 响应,IE 和 Mozilla 就会接受它,即使请求来自不同的域。您可以通过替换*
它们来缩小允许的请求域的范围。
回答by Chandresh
run it on any server like wamp or apache...
在 wamp 或 apache 等任何服务器上运行它...
it dont gives an error...
它不会给出错误...
NS_ERROR_DOM_BAD_URI: Access to restricted URI
回答by mkoryak
"I need to do cross domain Ajax request"
“我需要做跨域ajax请求”
You can't do that, unless:
你不能这样做,除非:
- you are using grease monkey
- you are making a jsonp request which isnt really ajax
- 你在用油猴
- 您正在发出一个实际上不是 ajax 的 jsonp 请求